【问题标题】:Receive hierarchy using Dictionary<string, dynamic> item使用 Dictionary<string, dynamic> 项接收层次结构
【发布时间】:2015-12-23 16:45:27
【问题描述】:

我需要处理一个包含完整层次结构的对象数组。 Like Country (USA) > State (Texas) > County (Harris) > City > Street (有很多房产)。

我使用了Dictionary&lt;string, dynamic&gt;,但我无法处理任何主要对象的子对象。所以按照上面的例子,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&lt;string, dynamic&gt;

【问题讨论】:

  • 请提供您尝试过但不起作用的代码。
  • 我有以下控制器方法签名: public ActionResult UploadApplication(List> Country) 所有 Country 属性都填充得很好,但是当我想访问“States”时据说是一个对象,无法将其转换为另一个 Dictionary
  • 请编辑您的问题以包含所有所需信息。不要将代码放在 cmets 中。关于 SO 的问题应该是独立的。
  • 我能够编辑帖子,抱歉,我不经常使用 stackoverflow。 :)
  • 你在使用asp.net-mvc吗?如果是这样,请标记您的问题。另外,您使用的是哪个版本?

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


【解决方案1】:

如果您的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 列表将包含具有所提供属性的动态对象。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 2012-03-01
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 2021-05-12
      相关资源
      最近更新 更多