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

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

    1. <legend id='h5bqV'><style id='h5bqV'><dir id='h5bqV'><q id='h5bqV'></q></dir></style></legend>

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

        使用 Json.NET 反序列化键值对数组

        时间:2023-08-23
          1. <small id='6IIgc'></small><noframes id='6IIgc'>

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

                    <tbody id='6IIgc'></tbody>
                  <tfoot id='6IIgc'></tfoot>
                1. 本文介绍了使用 Json.NET 反序列化键值对数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  给定以下 json:

                  [ {"id":"123", ... "data":[{"key1":"val1"}, {"key2":"val2"}], ...},... ]

                  这是更大树的一部分,我如何将数据"属性反序列化为:

                  List数据{得到;放;}

                  List数据{得到;放;}

                  字典<字符串,字符串>数据{得到;放;}

                  使用 Json.NET?任何一个版本都可以(尽管我更喜欢 List of MyCustomClass).我已经有一个包含其他属性的类,如下所示:

                  公共类 SomeData{[JsonProperty("_id")]公共字符串 ID { 获取;放;}...公共列表<MyCustomClass>数据{得到;放;}}

                  其中MyCustomClass"将仅包含两个属性(键和值).我注意到有一个 KeyValuePairConverter 类听起来可以满足我的需要,但我找不到如何使用它的示例.谢谢.

                  解决方案

                  最简单的方法是将键值对数组反序列化为IDictionary:

                  <上一页><代码>公共类 SomeData{公共字符串 ID { 获取;放;}public IEnumerable<IDictionary<string, string>>数据{得到;放;}}私人静态无效主要(字符串[] args){var json = "{ "id": "123", "data": [ { "key1": "val1" }, { "key2" : "val2"} ] }";var obj = JsonConvert.DeserializeObject(json);}

                  但如果您需要将其反序列化为您自己的类,它可能看起来像这样:

                  <上一页><代码>公共类 SomeData2{公共字符串 ID { 获取;放;}公共列表<SomeDataPair>数据{得到;放;}}公共类 SomeDataPair{公共字符串密钥 { 获取;放;}公共字符串值 { 获取;放;}}私人静态无效主要(字符串[] args){var json = "{ "id": "123", "data": [ { "key1": "val1" }, { "key2" : "val2"} ] }";var rawObj = JObject.Parse(json);var obj2 = 新的 SomeData2{Id = (string)rawObj["id"],数据 = 新列表()};foreach(rawObj["data"] 中的变量项){foreach(项目中的 var 道具){var property = prop as JProperty;如果(属性!= null){obj2.Data.Add(new SomeDataPair() { Key = property.Name, Value = property.Value.ToString() });}}}}

                  看到我知道 Valuestring 并且我调用 ToString() 方法,可以有另一个复杂的类.

                  Given the following json:

                  [ {"id":"123", ... "data":[{"key1":"val1"}, {"key2":"val2"}], ...}, ... ]
                  

                  that is part of a bigger tree, how can I deserialize the "data" property into:

                  List<MyCustomClass> Data { get; set; }
                  

                  or

                  List<KeyValuePair> Data { get; set; }
                  

                  or

                  Dictionary<string, string> Data { get; set; }
                  

                  using Json.NET? Either version will do (I prefer List of MyCustomClass though). I already have a class that contains other properties, like this:

                  public class SomeData
                  {
                     [JsonProperty("_id")]
                     public string Id { get; set; }
                     ...
                     public List<MyCustomClass> Data { get; set; }
                  }
                  

                  where "MyCustomClass" would include just two properties (Key and Value). I noticed there is a KeyValuePairConverter class that sounds like it would do what I need, but I wasn't able to find an example on how to use it. Thanks.

                  解决方案

                  The simplest way is deserialize array of key-value pairs to IDictionary<string, string>:

                  
                  public class SomeData
                  {
                      public string Id { get; set; }
                  
                      public IEnumerable<IDictionary<string, string>> Data { get; set; }
                  }
                  
                  private static void Main(string[] args)
                  {
                      var json = "{ "id": "123", "data": [ { "key1": "val1" }, { "key2" : "val2" } ] }";
                  
                      var obj = JsonConvert.DeserializeObject<SomeData>(json);
                  }
                  
                  

                  But if you need deserialize that to your own class, it can be looks like that:

                  
                  public class SomeData2
                  {
                      public string Id { get; set; }
                  
                      public List<SomeDataPair> Data { get; set; }
                  }
                  
                  public class SomeDataPair
                  {
                      public string Key { get; set; }
                  
                      public string Value { get; set; }
                  }
                  
                  private static void Main(string[] args)
                  {
                      var json = "{ "id": "123", "data": [ { "key1": "val1" }, { "key2" : "val2" } ] }";
                  
                      var rawObj = JObject.Parse(json);
                  
                      var obj2 = new SomeData2
                      {
                          Id = (string)rawObj["id"],
                          Data = new List<SomeDataPair>()
                      };
                  
                      foreach (var item in rawObj["data"])
                      {
                          foreach (var prop in item)
                          {
                              var property = prop as JProperty;
                  
                              if (property != null)
                              {
                                  obj2.Data.Add(new SomeDataPair() { Key = property.Name, Value = property.Value.ToString() });
                              }
                  
                          }
                      }
                  }
                  
                  

                  See that I khow that Value is string and i call ToString() method, there can be another complex class.

                  这篇关于使用 Json.NET 反序列化键值对数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 Json.net 将大量数据流式传输为 JSON 格式 下一篇:如何将参数传递给非默认构造函数?

                  相关文章

                  最新文章

                  <tfoot id='kRjd1'></tfoot>
                    <bdo id='kRjd1'></bdo><ul id='kRjd1'></ul>
                  <legend id='kRjd1'><style id='kRjd1'><dir id='kRjd1'><q id='kRjd1'></q></dir></style></legend>

                2. <small id='kRjd1'></small><noframes id='kRjd1'>

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