【发布时间】:2015-07-28 07:31:19
【问题描述】:
我的 Web API 正在发送这个 JSON。
{ "data":[
{
"cat_id":"1",
"category":"Clothing",
"img_url":"sampleUrl.com"
},
{
"cat_id":"2",
"category":"Electronic Shop",
"img_url":"sampleUrl.com"
},
{
"cat_id":"3",
"category":"Grocery",
"img_url":"sampleUrl.com"
},
{
"cat_id":"4",
"category":"Hardware",
"img_url":"sampleUrl.com"
}
]}
我尝试使用下面的 c# 代码反序列化这个 JSON
var result = JsonConvert.DeserializeObject<List<AllCategories>>(content);
但它会引发异常。
无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[EzyCity.AllCategories]' 因为该类型需要 JSON 数组(例如 [1 ,2,3]) 以正确反序列化。
AllCategories 类
public class AllCategories
{
private string cat_id;
private string category;
private string img_url;
public string Cat_Id
{
get { return cat_id; }
set { cat_id = value; }
}
public string Category
{
get { return category; }
set { category = value; }
}
public string Image_Url
{
get { return img_url; }
set { img_url = value; }
}
}
应该如何反序列化这种类型的 JSON?
【问题讨论】:
标签: c# json deserialization