【发布时间】:2014-11-12 03:38:04
【问题描述】:
我得到一个 JSON 字符串作为 HTTP 响应。这个字符串看起来像:
response: {
count: 524,
items: [{
id: 318936948,
owner_id: 34,
artist: 'The Smiths',
title: 'How Soon Is Now',
duration: 233,
url: 'link',
genre_id: 9
}, {
id: 312975563,
owner_id: 34,
artist: 'Thom Yorke',
title: 'Guess Again!',
duration: 263,
url: 'link',
genre_id: 22
}]
}
我有 Newtonsoft.Json 库,以及类 Response 和 Item:
[JsonObject(MemberSerialization.OptIn)]
class Response
{
[JsonProperty("count")]
public int count { get; set; }
[JsonProperty("items")]
public List<Item> items { get; set; }
}
[JsonObject(MemberSerialization.OptOut)]
class Item
{
public string aid { get; set; }
public string owner_id { get; set; }
public string artist { get; set; }
public string title { get; set; }
public string duration { get; set; }
public string url { get; set; }
public int lyrics_id { get; set; }
public int album_id { get; set; }
public int genre_id { get; set; }
}
我像这样反序列化它:
Response r = JsonConvert.DeserializeObject<Response>(line);
它不起作用,“r”保持为空。我错在哪里,为什么?正在编译,没有任何异常。
【问题讨论】:
-
当我将您的 Json 粘贴到 jsonformatter.curiousconcept.com 时出现一堆错误,例如“字符串应该用双引号括起来。[代码 17,结构 2]”。
-
Json 似乎是错误的。在这里查看json2csharp.com
-
另外,看起来
response是某些包含结构中的字段/属性,但您没有显示出来。