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

      <small id='9OZid'></small><noframes id='9OZid'>

        <bdo id='9OZid'></bdo><ul id='9OZid'></ul>
    1. <legend id='9OZid'><style id='9OZid'><dir id='9OZid'><q id='9OZid'></q></dir></style></legend>

    2. 将 JSON 数组反序列化为强类型的 .NET 对象

      时间:2023-08-22
      • <bdo id='RcAmW'></bdo><ul id='RcAmW'></ul>

        • <tfoot id='RcAmW'></tfoot>
          <legend id='RcAmW'><style id='RcAmW'><dir id='RcAmW'><q id='RcAmW'></q></dir></style></legend>

          1. <small id='RcAmW'></small><noframes id='RcAmW'>

                <tbody id='RcAmW'></tbody>
                <i id='RcAmW'><tr id='RcAmW'><dt id='RcAmW'><q id='RcAmW'><span id='RcAmW'><b id='RcAmW'><form id='RcAmW'><ins id='RcAmW'></ins><ul id='RcAmW'></ul><sub id='RcAmW'></sub></form><legend id='RcAmW'></legend><bdo id='RcAmW'><pre id='RcAmW'><center id='RcAmW'></center></pre></bdo></b><th id='RcAmW'></th></span></q></dt></tr></i><div id='RcAmW'><tfoot id='RcAmW'></tfoot><dl id='RcAmW'><fieldset id='RcAmW'></fieldset></dl></div>
                本文介绍了将 JSON 数组反序列化为强类型的 .NET 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                当我可以调用第 3 方 api 并取回单个类的数据时,一切都可以使用此代码进行反序列化

                When I can call the 3rd party api and get back a single class worth of data everything deserialises fine using this code

                TheUser me = jsonSerializer.Deserialize(response, typeof(TheUser)) as TheUser
                

                当我尝试反序列化作为数组的 JSON 响应内容时出现问题,例如

                The problem comes when I try and deserialise JSON response content that is an array, such as

                {
                   "data": [
                      {
                         "name": "A Jones",
                         "id": "500015763"
                      },
                      {
                         "name": "B Smith",
                         "id": "504986213"
                      },
                      {
                         "name": "C Brown",
                         "id": "509034361"
                      }
                   ]
                }
                

                只有在数据"成员周围使用自定义包装类并且该成员需要是 List 类型时,我才能使序列化工作.如果将它们作为类型 List<TheUser> 我从 JsonParser DesializeType 方法中得到 ArgumentException.

                I can only get the serialization to work if I use a custom wrapping class around the "data" member and that member needs to be of type List<object>. If it have them as type List<TheUser> I get ArgumentException from the JsonParser DesializeType method.

                我最初尝试在没有这样的包装类型的情况下进行序列化

                I originally tried to serialise without a wrapping type like this

                List<TheUser> freinds = jsonSerializer.Deserialize(response, typeof(List<TheUser>)) as List<TheUser>;
                

                但这只会给我一个空集合.当然,我必须能够将数组反序列化为强类型列表.

                but that just returns me an empty collection. Surely I must be able to have the array deserialize to a strongly typed list.

                推荐答案

                看完来源,对于 WP7 Hammock 实际上并没有使用 Json.Net 进行 JSON 解析.相反,它使用自己的解析器,不能很好地处理自定义类型.

                Afer looking at the source, for WP7 Hammock doesn't actually use Json.Net for JSON parsing. Instead it uses it's own parser which doesn't cope with custom types very well.

                如果直接使用 Json.Net,则可以反序列化为包装对象内的强类型集合.

                If using Json.Net directly it is possible to deserialize to a strongly typed collection inside a wrapper object.

                var response = @"
                    {
                        ""data"": [
                            {
                                ""name"": ""A Jones"",
                                ""id"": ""500015763""
                            },
                            {
                                ""name"": ""B Smith"",
                                ""id"": ""504986213""
                            },
                            {
                                ""name"": ""C Brown"",
                                ""id"": ""509034361""
                            }
                        ]
                    }
                ";
                
                var des = (MyClass)Newtonsoft.Json.JsonConvert.DeserializeObject(response, typeof(MyClass));
                
                return des.data.Count.ToString();
                

                并与:

                public class MyClass
                {
                    public List<User> data { get; set; }
                }
                
                public class User
                {
                    public string name { get; set; }
                    public string id { get; set; }
                }
                

                必须使用 data 属性创建额外的对象很烦人,但这是 JSON 格式对象的构造方式的结果.

                Having to create the extra object with the data property is annoying but that's a consequence of the way the JSON formatted object is constructed.

                文档:序列化和反序列化 JSON

                Documentation: Serializing and Deserializing JSON

                这篇关于将 JSON 数组反序列化为强类型的 .NET 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                      • <bdo id='doxZQ'></bdo><ul id='doxZQ'></ul>

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

                        <tfoot id='doxZQ'></tfoot>

                        <legend id='doxZQ'><style id='doxZQ'><dir id='doxZQ'><q id='doxZQ'></q></dir></style></legend>
                            <tbody id='doxZQ'></tbody>