我得到了一个JSON响应,例如:
JSON
"meta": {
"code": 200
},
"data": [
{
"username": "username here",
"bio": "bio here",
"website": "web site here",
"profile_picture": "link here",
"full_name": "name here",
"id": "id here"
}
]
C#
public class Meta
{
public int code { get; set; }
}
public class Datum
{
public string username { get; set; }
public string bio { get; set; }
public string website { get; set; }
public string profile_picture { get; set; }
public string full_name { get; set; }
public string id { get; set; }
}
public class RootObject
{
public Meta meta { get; set; }
public List<Datum> data { get; set; }
}
我写了这段代码:
JObject instaCall = JObject.Parse(response);
Datum searchResult = instaCall["data"].ToObject<Datum>();
但会产生错误:
无法将当前JSON数组(例如[1,2,3])反序列化为类型“WindowsFormsApplication1.functions.response Datum”,因为该类型需要JSON对象(例如{“name”:“value”})才能正确反序列化 . 要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList),例如List从JSON数组反序列化 . JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化 .
2 回答
这样的事情怎么样:
正如错误试图告诉你的那样,
instaCall["data"]
是一个数组 .您无法将其读入单个对象 .