在json.net中,如何反序列化这个json:
{
"items": {
"count": 1,
"data": [
{
...
},
...
],
}
}
到这个C#类:
class MyData
{
[JsonProperty("items/data")] // this doesn't work
public List<object> Items { get; set; }
}
-
// Then this doesn't populate Items
MyData data = JsonConvert.DeserializeObject<MyData>(json);
使用json.net有一种优雅的方法吗?
答案 0 :(得分:1)
我只想到了如何做到这一点:
var jObject = JObject.Parse(json);
jObject["items"] = jObject["items"]["data"];
MyData data = JsonConvert.DeserializeObject<MyData>(jObject.ToString());
任何人都有更好的解决方案吗?
答案 1 :(得分:0)
对于填充项目,你应该有这个
public class Datum
{
}
public class Items
{
public int count { get; set; }
public List<Datum> data { get; set; }
}
public class RootObject
{
public Items items { get; set; }
}
然后使用它:
MyData data = JsonConvert.DeserializeObject<RootObject>(json);