C# Json.Net反序列化会导致空对象
我正在对json响应进行反序列化,并得到空值:C# Json.Net反序列化会导致空对象,c#,json,C#,Json,我正在对json响应进行反序列化,并得到空值: { "response": { "status": { "version": "4.2", "code": 0, "message": "Success" }, "start": 0, "total": 12, "biographies": [{ "text": "\
{
"response": {
"status": {
"version": "4.2",
"code": 0,
"message": "Success"
},
"start": 0,
"total": 12,
"biographies": [{
"text": "\"Radiohead\" are an English alternative rock band from Abingdon, Oxfordshire, formed in 1985. The band consists of Thom Yorke (vocals, guitars, piano), Jonny Greenwood (guitars, keyboards, other instruments), Ed O'Brien (guitars, backing vocals), Colin Greenwood (bass, synthesisers) and Phil Selway (drums, percussion).",
"site": "wikipedia",
"url": "http://en.wikipedia.org/wiki/Radiohead",
"license": {
"type": "cc-by-sa",
"attribution": "n/a",
"url": ""
}
}]
}
}
这是我用来序列化上面的json的类
public class Response : IResponse
{
public Status status { get; set; }
public string start { get; set; }
public string total { get; set; }
public List<Biography> biographies { get; set; }
}
为什么响应
类的所有值都返回null
在这种情况下,T
是Response
:
return string.IsNullOrEmpty(response) ? default(T) : JsonConvert.DeserializeObject<T>(response);
返回字符串.IsNullOrEmpty(响应)?默认值(T):JsonConvert.DeserializeObject(响应);
为了反序列化,您需要创建一个包含响应
属性的类
public class RootObject
{
public Response response { get; set; }
}
然后使用
RootObject
类进行反序列化。将您的json放在这里,获取完整的类。@Arshad感谢您的分享,该站点太棒了!
public class RootObject
{
public Response response { get; set; }
}