<legend id='HOEpL'><style id='HOEpL'><dir id='HOEpL'><q id='HOEpL'></q></dir></style></legend>
  • <i id='HOEpL'><tr id='HOEpL'><dt id='HOEpL'><q id='HOEpL'><span id='HOEpL'><b id='HOEpL'><form id='HOEpL'><ins id='HOEpL'></ins><ul id='HOEpL'></ul><sub id='HOEpL'></sub></form><legend id='HOEpL'></legend><bdo id='HOEpL'><pre id='HOEpL'><center id='HOEpL'></center></pre></bdo></b><th id='HOEpL'></th></span></q></dt></tr></i><div id='HOEpL'><tfoot id='HOEpL'></tfoot><dl id='HOEpL'><fieldset id='HOEpL'></fieldset></dl></div>
    1. <small id='HOEpL'></small><noframes id='HOEpL'>

        <bdo id='HOEpL'></bdo><ul id='HOEpL'></ul>

    2. <tfoot id='HOEpL'></tfoot>

        如何反序列化具有动态(数字)键名的子对象?

        时间:2023-08-23
          • <bdo id='i8xaR'></bdo><ul id='i8xaR'></ul>
            <i id='i8xaR'><tr id='i8xaR'><dt id='i8xaR'><q id='i8xaR'><span id='i8xaR'><b id='i8xaR'><form id='i8xaR'><ins id='i8xaR'></ins><ul id='i8xaR'></ul><sub id='i8xaR'></sub></form><legend id='i8xaR'></legend><bdo id='i8xaR'><pre id='i8xaR'><center id='i8xaR'></center></pre></bdo></b><th id='i8xaR'></th></span></q></dt></tr></i><div id='i8xaR'><tfoot id='i8xaR'></tfoot><dl id='i8xaR'><fieldset id='i8xaR'></fieldset></dl></div>

            <legend id='i8xaR'><style id='i8xaR'><dir id='i8xaR'><q id='i8xaR'></q></dir></style></legend>
                    <tbody id='i8xaR'></tbody>
                1. <tfoot id='i8xaR'></tfoot>

                  <small id='i8xaR'></small><noframes id='i8xaR'>

                  本文介绍了如何反序列化具有动态(数字)键名的子对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何反序列化此 JSON 数据?100034"等键本质上是动态的.

                  How can I deserialize this JSON data? The keys "100034" etc. are dynamic in nature.

                  {
                      "users" : {
                          "100034" : {
                              "name"  : "tom",
                              "state" : "WA",
                              "id"    : "cedf-c56f-18a4-4b1"
                          },
                          "10045" : {
                              "name"  : "steve",
                              "state" : "NY",
                              "id"    : "ebb2-92bf-3062-7774"
                          },
                          "12345" : {
                              "name"  : "mike",
                              "state" : "MA",
                              "id"    : "fb60-b34f-6dc8-aaf7"
                          }
                      }
                  }
                  

                  有没有一种方法可以直接访问具有名称、状态和 ID 的每个对象?

                  Is there a way I can directly access each object having name, state and Id?

                  推荐答案

                  对于属性名称可变的 JSON 对象,您可以使用 Dictionary 代替常规类,其中 T 是表示项目数据的类.

                  For JSON objects having property names which can vary, you can use a Dictionary<string, T> in place of a regular class, where T is a class representing the item data.

                  像这样声明你的类:

                  class RootObject
                  {
                      public Dictionary<string, User> users { get; set; }
                  }
                  
                  class User
                  {
                      public string name { get; set; }
                      public string state { get; set; }
                      public string id { get; set; }
                  }
                  

                  然后像这样反序列化:

                  RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
                  

                  演示:

                  class Program
                  {
                      static void Main(string[] args)
                      {
                          string json = @"
                          {
                              ""users"": {
                                  ""10045"": {
                                      ""name"": ""steve"",
                                      ""state"": ""NY"",
                                      ""id"": ""ebb2-92bf-3062-7774""
                                  },
                                  ""12345"": {
                                      ""name"": ""mike"",
                                      ""state"": ""MA"",
                                      ""id"": ""fb60-b34f-6dc8-aaf7""
                                  },
                                  ""100034"": {
                                      ""name"": ""tom"",
                                      ""state"": ""WA"",
                                      ""id"": ""cedf-c56f-18a4-4b1""
                                  }
                              }
                          }";
                  
                          RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
                  
                          foreach (string key in root.users.Keys)
                          {
                              Console.WriteLine("key: " + key);
                              User user = root.users[key];
                              Console.WriteLine("name: " + user.name);
                              Console.WriteLine("state: " + user.state);
                              Console.WriteLine("id: " + user.id);
                              Console.WriteLine();
                          }
                      }
                  }
                  

                  输出:

                  key: 10045
                  name: steve
                  state: NY
                  id: ebb2-92bf-3062-7774
                  
                  key: 12345
                  name: mike
                  state: MA
                  id: fb60-b34f-6dc8-aaf7
                  
                  key: 100034
                  name: tom
                  state: WA
                  id: cedf-c56f-18a4-4b1
                  

                  这篇关于如何反序列化具有动态(数字)键名的子对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 JSON 反序列化为 C# 类 下一篇:使用 Json.Net 序列化时指定自定义 DateTime 格式

                  相关文章

                  最新文章

                  <small id='EZTHL'></small><noframes id='EZTHL'>

                    <bdo id='EZTHL'></bdo><ul id='EZTHL'></ul>

                      <tfoot id='EZTHL'></tfoot>

                      <legend id='EZTHL'><style id='EZTHL'><dir id='EZTHL'><q id='EZTHL'></q></dir></style></legend>
                    1. <i id='EZTHL'><tr id='EZTHL'><dt id='EZTHL'><q id='EZTHL'><span id='EZTHL'><b id='EZTHL'><form id='EZTHL'><ins id='EZTHL'></ins><ul id='EZTHL'></ul><sub id='EZTHL'></sub></form><legend id='EZTHL'></legend><bdo id='EZTHL'><pre id='EZTHL'><center id='EZTHL'></center></pre></bdo></b><th id='EZTHL'></th></span></q></dt></tr></i><div id='EZTHL'><tfoot id='EZTHL'></tfoot><dl id='EZTHL'><fieldset id='EZTHL'></fieldset></dl></div>