【发布时间】:2015-02-23 02:32:43
【问题描述】:
我有一个类似 JSON 的
{
"40": {
"name": "Team A vs Team B",
"value": {
"home": 1,
"away": 0
}
},
"45": {
"name": "Team A vs Team C",
"value": {
"home": 2,
"away": 0
}
},
"50": {
"name": "Team A vs Team D",
"value": {
"home": 0,
"away": 2
}
}
}
所以这是一种匹配列表。我有类将其反序列化为:
public class Match
{
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "value")]
public Value Values { get; set; }
}
public class Value
{
[JsonProperty(PropertyName = "home")]
public int Home { get; set; }
[JsonProperty(PropertyName = "away")]
public int Away { get; set; }
}
我正在尝试像这样反序列化 json:
var mList= JsonConvert.DeserializeObject<List<Match>>(jsonstr);
但我遇到了异常:
无法反序列化当前 JSON 对象(例如 {"name":"value"}) 进入类型“System.Collections.Generic.List`1[ClassNameHere]”,因为 该类型需要一个 JSON 数组(例如 [1,2,3])来反序列化 正确。
如果我像这样更改代码:
var mList= JsonConvert.DeserializeObject(jsonstr);
然后它序列化但不是作为一个列表,作为一个对象。我怎样才能解决这个问题?
【问题讨论】:
-
在 jsonstr 的开头和结尾放一个 [](括号)
标签: c# json json.net json-deserialization