如何使用DeserializeObject和JsonProperty将JSON反序列化到列表中,而元素的索引是非整数?
JSON:
{
"success": true,
"TEST": {
"360450323_188530139": {
"id": "24216",
"name": "zxc",
"desc": "cxz"
},
"310777518_0": {
"id": "6458678634",
"name": "dfgsfd",
"desc": "sdfxcvnbhr"
}
}
}
有没有办法从中制作清单?
我试过了:
using(WebClient wc = new WebClient())
{
var url = "...";
var json = wc.DownloadString(url);
Result result = JsonConvert.DeserializeObject<Result>(json);
}
public class Result
{
[JsonProperty("success")]
public string Success { get; set; }
[JsonProperty("TEST")]
public List<Test> Tests{ get; set; }
}
public class Test
{
[JsonProperty("id")]
public int id { get; set;}
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("desc")]
public string Desc { get; set; }
}
答案 0 :(得分:1)
您在JSON中有一个键控集合,可以轻松映射到字典。你可以检查result.Test.Values
using(WebClient wc = new WebClient())
{
var url = "...";
var json = wc.DownloadString(url);
Result result = JsonConvert.DeserializeObject<Result>(json);
// result.Values should contain the Test instances at this point.
}
public class Result
{
[JsonProperty("success")]
public string Success { get; set; }
[JsonProperty("TEST")]
public Dictionary<string,Test> Tests{ get; set; }
}
public class Test
{
// omitted - same as in question.
}