使用Dictionary <string,dynamic =“”> item接收层次结构

时间:2015-12-23 16:45:28

标签: c# json asp.net-mvc-4 dictionary

我需要处理一个包含完整层次结构的对象数组。喜欢国家(美国)&gt;州(德克萨斯州)&gt;县(哈里斯)&gt;城市&gt;街(有很多房产)。

我使用了Dictionary<string, dynamic>,但我无法处理任何主要对象的子对象。因此,按照上面的示例,Country处理得很好,但我不知道如何在States对象中获取Country

我想处理以下JSON:

{
  "Country": [
    {
      "Property1": "",
      "Property2": "",
      "Property3": "",
      "Property4": "",
      "Property5": "",
      "States": [
        { "Prop1": "", "Prop2": "", "Prop3": "", "Prop4": "" },
        { "Prop1": "", "Prop2": "", "Prop3": "", "Prop4": "" }
      ]
    },
    {
      "Property1": "",
      "Property2": "",
      "Property3": "",
      "Property4": "",
      "Property5": "",
      "States": [
        { "Prop1": "", "Prop2": "", "Prop3": "", "Prop4": "" },
        { "Prop1": "", "Prop2": "", "Prop3": "", "Prop4": "" }
      ]
    }
  ]
}

我有以下控制器方法签名:

public ActionResult UploadApplication(List<Dictionary<string, dynamic>> Country)

所有Country属性都填充得很好,但是当我想访问States时,它会说一个对象,并且无法将其转换为另一个Dictionary<string, dynamic> }。

2 个答案:

答案 0 :(得分:0)

为您的JSON结构创建静态合约:

public class RootObject
{
    public List<Country> Country { get; set; }
}
public class Country
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
    public string Property4 { get; set; }
    public string Property5 { get; set; }

    public List<State> States { get; set; }
}
public class State
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
    public string Prop4 { get; set; }
}

然后,您可以像这样解析JSON字符串:

JavaScriptSerializer serializer = new JavaScriptSerializer();
RootObject obj = serializer.Deserialize<RootObject>(json);

// example access
Console.WriteLine(obj.Country[0].States[0].Prop1);

答案 1 :(得分:0)

如果您的State没有固定的结构(词典中dynamic让我考虑到这一点),您可以像这样编写代码:

    public class Container
    {
        public List<Country> Country { get; set; }
    }
    public class Country
    {
        public string Property1 { get; set; }
        public string Property2 { get; set; }
        public string Property3 { get; set; }
        public string Property4 { get; set; }
        public string Property5 { get; set; }

        public List<dynamic> States { get; set; }
    }

    //public class State
    //{
    //    public string Prop1 { get; set; }
    //    public string Prop2 { get; set; }
    //    public string Prop3 { get; set; }
    //    public string Prop4 { get; set; }
    //}

var jsonData = JsonConvert.DeserializeObject<Container>(@"
{
  ""Country"": [
    {
      ""Property1"": ""p1"",
      ""Property2"": ""p2"",
      ""Property3"": ""p3"",
      ""Property4"": ""p4"",
      ""Property5"": ""p5"",
      ""States"": [
        { ""Prop1"": ""p11"", ""Prop2"": ""p12"", ""Prop3"": ""p13"", ""Prop4"": ""p14"" },
        { ""Prop1"": ""p21"", ""Prop2"": ""p22"", ""Prop3"": ""p23"", ""Prop4"": ""p24"", ""Prop5"": ""p25"" }
      ]
    },
    {
      ""Property1"": """",
      ""Property2"": """",
      ""Property3"": """",
      ""Property4"": """",
      ""Property5"": """",
      ""States"": [
        { ""Prop1"": """", ""Prop2"": """", ""Prop3"": """", ""Prop4"": """" },
        { ""Prop1"": """", ""Prop2"": """", ""Prop3"": """", ""Prop4"": """" }
      ]
    }
  ]
}
                ");

您的States列表将包含具有所提供属性的动态对象。