反序列化JSON数组强类型的.NET对象 [英] Deserializing JSON array into strongly typed .NET object

查看:182
本文介绍了反序列化JSON数组强类型的.NET对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我可以调用第三方的API,并取回一个类有价值的数据都使用这种code deserialises罚款

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

问题是当我尝试和deserialise 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"
      }
   ]
}

我只能得到系列化工作,如果我周围使用数据成员定制的包装类和成员必须键入列表与LT的;对象> 。如果让他们为类型列表< TheUser> 我得到的ArgumentException JsonParser DesializeType 方法。

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.

推荐答案

AFER在看<一个href=\"http://hammock.$c$cplex.com/SourceControl/changeset/view/fed040573260#src/net40/Hammock.WindowsPhone/Hammock.WindowsPhone.csproj\">the来源,对于WP7吊床实际上并未使用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; }
}

不必创建额外的对象和数据属性是烦人,但是那是的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对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆