【发布时间】:2014-10-23 13:15:11
【问题描述】:
我已经开始研究了一段时间,但似乎无法完全理解它,本质上我有一个 JSON 数组,我想将其解码为 Notifications 对象,例外情况是:
“在 Newtonsoft.Json.dll 中发生了“Newtonsoft.Json.JsonSerializationException”类型的未处理异常 附加信息:无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“WpfApplication2.Notifications”,因为该类型需要 JSON 对象(例如 {"name":"value"})才能正确反序列化。
public Notifications Notes;
// HTTP request gets the JSON below.
//EXCEPTION ON THIS LINE
Notes = JsonConvert.DeserializeObject<Notifications>(responseString);
public class Notifications
{
[JsonProperty("note_id")]
public int note_id { get; set;}
[JsonProperty("sender_id")]
public int sender_id { get; set; }
[JsonProperty("receiver_id")]
public int receiver_id { get; set; }
[JsonProperty("document_id")]
public int document_id { get; set; }
[JsonProperty("search_name")]
public string search_name { get; set; }
[JsonProperty("unread")]
public int unread { get; set; }
}
检索到的Json是:
[
{
"note_id": "5",
"sender_id": "3",
"receiver_id": "1",
"document_id": "102",
"unread": "1"
},
{
"note_id": "4",
"sender_id": "2",
"receiver_id": "1",
"document_id": "101",
"unread": "1"
}
]
【问题讨论】:
-
可能是因为您的数组未命名,即它应该类似于
{ "arrayname": [ {...}, {...} ] }? -
需要反序列化为数组或集合