C# annoymous object contain array

private void SerializeDemo()
{
    var obj = new[]
    {
        new
        {
            CountryId=1,
            CountryName="USA",
            StateList= new []
            {
                    new[]
                    {
                        new
                        {
                            StateId=1,
                            StateName="NY",
                            CityList=new []
                            {
                                new []
                                {
                                    new
                                    {
                                        CityId=1,
                                        CityName="NYC1"
                                    },
                                    new
                                    {
                                        CityId=2,
                                        CityName="NYC2"
                                    }
                                }
                            }
                        },
                        new
                        {
                            StateId=2,
                            StateName="CA",
                            CityList=new []
                            {
                                new []
                                {
                                    new
                                    {
                                        CityId=1,
                                        CityName="CA1"
                                    },
                                    new
                                    {
                                        CityId=2,
                                        CityName="CA2"
                                    }
                                }
                            }
                        },
                        new
                        {
                            StateId=3,
                            StateName="FL",
                            CityList=new[]
                            {
                                 new []
                                {
                                    new
                                    {
                                        CityId=1,
                                        CityName="FL1"
                                    },
                                    new
                                    {
                                        CityId=2,
                                        CityName="FL2"
                                    }
                                }
                            }
                        }

                    }, 
            }
        },
        new
        {
            CountryId=2,
            CountryName="JP",
            StateList= new []
            {
                    new[]
                    {
                        new
                        {
                            StateId=1,
                            StateName="TK",
                            CityList=new []
                            {
                                new []
                                {
                                    new
                                    {
                                        CityId=1,
                                        CityName="TK1"
                                    },
                                    new
                                    {
                                        CityId=2,
                                        CityName="TK2"
                                    }
                                }
                            }
                        },
                        new
                        {
                            StateId=2,
                            StateName="TY",
                            CityList=new []
                            {
                                new []
                                {
                                    new
                                    {
                                        CityId=1,
                                        CityName="TY1"
                                    },
                                    new
                                    {
                                        CityId=2,
                                        CityName="TY2"
                                    }
                                }
                            }
                        },
                        new
                        {
                            StateId=3,
                            StateName="DJ",
                            CityList=new[]
                            {
                                 new []
                                {
                                    new
                                    {
                                        CityId=1,
                                        CityName="DJ1"
                                    },
                                    new
                                    {
                                        CityId=2,
                                        CityName="DJ2"
                                    }
                                }
                            }
                        } 
                    },
            }
        }
    }; 
    string jsonStr = JsonConvert.SerializeObject(obj, Formatting.Indented);
    System.Diagnostics.Debug.WriteLine(jsonStr);
}

 

[
  {
    "CountryId": 1,
    "CountryName": "USA",
    "StateList": [
      [
        {
          "StateId": 1,
          "StateName": "NY",
          "CityList": [
            [
              {
                "CityId": 1,
                "CityName": "NYC1"
              },
              {
                "CityId": 2,
                "CityName": "NYC2"
              }
            ]
          ]
        },
        {
          "StateId": 2,
          "StateName": "CA",
          "CityList": [
            [
              {
                "CityId": 1,
                "CityName": "CA1"
              },
              {
                "CityId": 2,
                "CityName": "CA2"
              }
            ]
          ]
        },
        {
          "StateId": 3,
          "StateName": "FL",
          "CityList": [
            [
              {
                "CityId": 1,
                "CityName": "FL1"
              },
              {
                "CityId": 2,
                "CityName": "FL2"
              }
            ]
          ]
        }
      ]
    ]
  },
  {
    "CountryId": 2,
    "CountryName": "JP",
    "StateList": [
      [
        {
          "StateId": 1,
          "StateName": "TK",
          "CityList": [
            [
              {
                "CityId": 1,
                "CityName": "TK1"
              },
              {
                "CityId": 2,
                "CityName": "TK2"
              }
            ]
          ]
        },
        {
          "StateId": 2,
          "StateName": "TY",
          "CityList": [
            [
              {
                "CityId": 1,
                "CityName": "TY1"
              },
              {
                "CityId": 2,
                "CityName": "TY2"
              }
            ]
          ]
        },
        {
          "StateId": 3,
          "StateName": "DJ",
          "CityList": [
            [
              {
                "CityId": 1,
                "CityName": "DJ1"
              },
              {
                "CityId": 2,
                "CityName": "DJ2"
              }
            ]
          ]
        }
      ]
    ]
  }
]

 

posted @ 2024-07-18 18:14  FredGrit  阅读(4)  评论(0编辑  收藏  举报