Json.NET:反序列化嵌套Json [英] Json.NET: Deserializing Nested Json
本文介绍了Json.NET:反序列化嵌套Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在此Json字符串上反序列化"Items"类部分:
How do I deserialize the "Items" class part on this Json string:
{
"Buddies": {
"count": 1,
"items": [
{
"id": "5099207ee4b0cfbb6a2bd4ec",
"firstName": "Foo",
"lastName": "Bar",
"image": {
"url": "",
"sizes": [
60,
120,
180,
240,
360
],
"name": "myphoto.png"
}
}
]
}
}
我原来的班级是:
public class Buddy
{
public IEnumerable<Item> Items { get; set; }
public class Item {
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
但是json的上半部分对我来说毫无用处,我想改用此类:
But the upper part of json is pretty useless to me and I want to use this class instead:
public class Buddy
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
推荐答案
Here's an approach using JSONPath, assuming your JSON is in a variable named json
:
var buddies = JObject.Parse(json).SelectToken("$.Buddies.items").ToObject<Buddy[]>();
这篇关于Json.NET:反序列化嵌套Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文