【发布时间】:2015-04-26 07:09:16
【问题描述】:
我想用 Newtonsoft Json.NET 解析一段 JSON
JSON:
[{
"type": "switchStatus",
"Data" :[
{
"ID" : "1",
"value" : "2.5"
},
{
"ID" : "2",
"value" : "4.2"
}
],
"Datetime": "2014-12-01",
"customerID": "50"
}]
类:
public class Account
{
[JsonProperty("type")]
public string Type { get; set; }
public List<Data> Data { get; set; }
[JsonProperty("Datetime")]
public string DateTime { get; set; }
[JsonProperty("customerID")]
public string CustomerId { get; set; }
}//Account
public class Data
{
[JsonProperty("ID")]
public string Id { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
解析:
Account account = JsonConvert.DeserializeObject<Account>(message);
错误:
无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“JSonParser.Account”,因为该类型需要 JSON 对象(例如 {"name":"value"})才能正确反序列化。
要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),例如可以从 JSON 数组反序列化的列表。也可以将 JsonArrayAttribute 添加到类型中以强制它从 JSON 数组中反序列化。
路径'',第 1 行,位置 1。
【问题讨论】: