【发布时间】:2016-05-14 13:39:49
【问题描述】:
我从 API 获取 Json 数据,我一直在尝试反序列化。
Json 数据:
{
"items": [
{
"id": "1",
"name": "samplename",
"AddressList1": {
"City": "Hyd",
"State": "TN",
"Country": "IN"
},
"Age": "10"
},
{
"id": "2",
"name": "samplename2",
"AddressList1": {
"City": "Hydd",
"State": "TN",
"Country": "IN"
},
"Age": "10"
}
],
"paging": {
"cursors": {}
}
}
实体:
public class AddressList1
{
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
public class Item
{
public string id { get; set; }
public string name { get; set; }
public AddressList1 addressList1 { get; set; }
public string Age { get; set; }
}
public class Cursors
{
}
public class Paging
{
public Cursors cursors { get; set; }
}
public class Users
{
public List<Item> items { get; set; }
public Paging paging { get; set; }
}
C#代码:
JsonConvert.DeserializeObject<List<Users>>(content);
错误信息:
无法反序列化当前 JSON 对象(例如 {"name":"value"}) 进入类型'System.Collections.Generic.List`1 [Entities.Users]' 因为该类型需要一个 JSON 数组(例如 [1,2,3])来反序列化 正确。
我哪里做错了?
【问题讨论】:
-
如果向下滚动,通常会在 json 中注明精确位置
-
我认为它认为 "items": [ ... ] 是一个 JArray。 JSON 是由序列化相同的模型制成的吗?
-
特别是,反序列化为
List<T>仅在顶级值是 JSON 中的数组时才有意义。在您的情况下,它不是 - 它是一个具有两个属性(items和paging)的对象。 -
@din 试过了。还是不行
标签: c# json serialization json.net deserialization