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

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

    2. 当键值未知时反序列化 JSON

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

              • <legend id='ADL75'><style id='ADL75'><dir id='ADL75'><q id='ADL75'></q></dir></style></legend>

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

              • <tfoot id='ADL75'></tfoot>

              • 本文介绍了当键值未知时反序列化 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如何在 C# 中使用 JSON.net 反序列化 JSON,其中键值未知(它们是多个设备的 MAC 地址).可能有一个或多个键条目.

                How do I deserialize JSON with JSON.net in C# where the key values are not known (they are MAC addresses of multiple devices). There could be one or more key entries.

                {
                    "devices":
                    {
                        "00-00-00-00-00-00-00-00":
                        {
                            "name":"xxx",
                            "type":"xxx",
                            "hardwareRevision":"1.0",
                            "id":"00-00-00-00-00-00-00-00"
                        },
                        "01-01-01-01-01-01-01-01":
                        {
                            "name":"xxx",
                            "type":"xxx",
                            "hardwareRevision":"1.0",
                            "id":"01-01-01-01-01-01-01-01"
                        },
                      }
                }
                

                推荐答案

                您可以使用字典将 MAC 地址存储为键:

                You can use a Dictionary to store the MAC addresses as keys:

                public class Device
                {
                    public string Name { get; set; }
                    public string Type { get; set; }
                    public string HardwareRevision { get; set; }
                    public string Id { get; set; }
                }
                
                public class Registry
                {
                    public Dictionary<string, Device> Devices { get; set; }
                }
                

                以下是反序列化示例 JSON 的方法:

                Here's how you could deserialize your sample JSON:

                Registry registry = JsonConvert.DeserializeObject<Registry>(json);
                
                foreach (KeyValuePair<string, Device> pair in registry.Devices)
                {
                    Console.WriteLine("MAC = {0}, ID = {1}", pair.Key, pair.Value.Id);
                }
                

                输出:

                MAC = 00-00-00-00-00-00-00-00, ID = 00-00-00-00-00-00-00-00
                MAC = 01-01-01-01-01-01-01-01, ID = 01-01-01-01-01-01-01-01
                

                这篇关于当键值未知时反序列化 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Json.NET 反序列化或序列化 json 字符串并将属性映 下一篇:Newtonsoft JSON 反序列化

                相关文章

                最新文章

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

                  <small id='1pQQb'></small><noframes id='1pQQb'>

                    <tfoot id='1pQQb'></tfoot><legend id='1pQQb'><style id='1pQQb'><dir id='1pQQb'><q id='1pQQb'></q></dir></style></legend>
                    • <bdo id='1pQQb'></bdo><ul id='1pQQb'></ul>