即时通讯从一个API获取Json数据,我一直试图反序列化。如何反序列化JSON数据? C#
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的精确位置注意 – Plutonix
我认为它认为“项目”:[...]是一个JArray。 JSON是由序列化相同的模型制成的? – Swen
特别是,如果顶级值是JSON中的数组,则对列表进行反序列化才有意义。在你的情况下,它不是 - 它是一个具有两个属性('items'和'paging')的对象。 –