使用Dictionary接收层次结构<;字符串,动态>;项目

本文关键字:字符串 动态 项目 gt lt Dictionary 层次结构 使用 | 更新日期: 2023-09-27 18:27:56

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

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

我想处理以下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>

使用Dictionary接收层次结构<;字符串,动态>;项目

如果您的State没有固定的结构(Dictionary中的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列表将包含具有所提供属性的动态对象。

为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);