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

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

        <tfoot id='Gp8JY'></tfoot>

        如何使用 NewtonSoft JsonConvert 反序列化名称中带有

        时间:2023-08-23

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

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

                <legend id='wDLi2'><style id='wDLi2'><dir id='wDLi2'><q id='wDLi2'></q></dir></style></legend>

                1. 本文介绍了如何使用 NewtonSoft JsonConvert 反序列化名称中带有破折号(“-")的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我们有一个 JSON 对象,其中一个对象的名称中有一个破折号.例如下面.

                  We have a JSON object with one of the object having a dash in its name. Ex below.

                  {
                      "veg": [
                          {
                              "id": "3",
                              "name": "Vegetables",
                              "count": "25"
                          },
                          {
                              "id": "4",
                              "name": "Dal",
                              "count": "2"
                          },
                          {
                              "id": "5",
                              "name": "Rice",
                              "count": "8"
                          },
                          {
                              "id": "7",
                              "name": "Breads",
                              "count": "6"
                          },
                          {
                              "id": "9",
                              "name": "Meals",
                              "count": "3"
                          },
                          {
                              "id": "46",
                              "name": "Extras",
                              "count": "10"
                          }
                      ],
                      "non-veg": [
                          {
                              "id": "25",
                              "name": "Starters",
                              "count": "9"
                          },
                          {
                              "id": "30",
                              "name": "Gravies",
                              "count": "13"
                          },
                          {
                              "id": "50",
                              "name": "Rice",
                              "count": "4"
                          }
                      ]
                  }
                  

                  我们如何反序列化这个 json?

                  How can we deserialize this json?

                  推荐答案

                  你可以使用DataContractJsonSerializer来实现

                  You can achieve this by using DataContractJsonSerializer

                  [DataContract]
                  public class Item
                  {
                      [DataMember(Name = "id")]
                      public int Id { get; set; }
                      [DataMember(Name = "name")]
                      public string Name { get; set; }
                      [DataMember(Name = "count")]
                      public int Count { get; set; }
                  }
                  
                  [DataContract]
                  public class ItemCollection
                  {
                      [DataMember(Name = "veg")]
                      public IEnumerable<Item> Vegetables { get; set; }
                      [DataMember(Name = "non-veg")]
                      public IEnumerable<Item> NonVegetables { get; set; }
                  }
                  

                  现在你可以用这样的方式反序列化它:

                  now you can deserialize it with something like this:

                  string data;
                  
                  // fill the json in data variable
                  
                  ItemCollection collection;
                  using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(data)))
                  {
                      DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ItemCollection));
                      collection = (ItemCollection)serializer.ReadObject(ms);
                  }
                  

                  这篇关于如何使用 NewtonSoft JsonConvert 反序列化名称中带有破折号(“-")的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:“Newtonsoft.Json"已经为“Microsoft.CSharp"定义 下一篇:无法使用 Json.NET 将枚举正确转换为 json

                  相关文章

                  最新文章

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

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

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