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

Deserializing JSON array into strongly typed .NET object(将 JSON 数组反序列化为强类型的 .NET 对象)
本文介绍了将 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 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)