我有一个json字符串items
,即
{
"items": [
{
"kind": "youtube#playlistItem",
"etag": "\"uQc-MPTsstrHkWLmeNsM/NAYVM2gp_1c4XbQ9zYswb1-_gnk\"",
"id": "UExGS2IhV2ZJM2tZMTg2MWZZeDR2cS41NEM3RjdGQ0FDRjkwNUQ5",
"snippet": {
"publishedAt": "2017-03-21T20:38:22.000Z",
"channelId": "UCRUNoQu8kdtsY94D9MZ_sXA",
"title": "News Plus 21-03-17",
"description": "News Plus 21-03-17",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/FIXv7nmU/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/FFynmU/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/FIXvnmU/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "",
"playlistId": "PLFKb5r-iLUmbOhkY1861fYx4vq",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "FIXvFylfjwmU"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"uQc-MPTsstrHkeNsM/TKjx1_sGvJVB3tX8cFnxsE\"",
"id": "UExGS2I1ci1pTFVtYk9oc2htryhjrtUNBMTM5RDMyREQ5",
"snippet": {
"publishedAt": "2017-03-21T19:34:48.000Z",
"channelId": "UCtsY94D9MZ_sXA",
"title": "Private video",
"description": "This video is private.",
"channelTitle": "Capital TV",
"playlistId": "PLFKb5r-iLUmbOh861fYx4vq",
"position": 1,
"resourceId": {
"kind": "youtube#video",
"videoId": "VQ1rDS59318"
}
}
},
{
"kind": "youtube#playlistItem",
"etag": "\"uQc-MPTsstrHkWLmeNsM/JXH9kcF8my3r2Yl0OPK3U\"",
"id": "UExGS2I1ci1pTFVtYhV2ZJM2tZMTg2MWZZeDR2BBNzZCQzY5MDk4",
"snippet": {
"publishedAt": "2017-03-21T19:06:41.000Z",
"channelId": "UCRUNtrhjsY94D9MZ_sXA",
"title": "Director NAB Corruption Scandal",
"description": "",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/BCnejgyQ/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/BCnejyQ/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/BCneyQ/hqdault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Capital TV",
"playlistId": "PLFKb5r-iLUmbOhkY1861fYx4vq",
"position": 2,
"resourceId": {
"kind": "youtube#video",
"videoId": "BCnhtjty2gyQ"
}
}
}
}
]
}
由于items
有两个元素,但我想计算snippet
中存在的元素数量,我如何计算,我已经解析了json并尝试获取长度,
dynamic result = JsonConvert.DeserializeObject("jsonStr");
foreach (var res in result.items)
{
int size = res.snippet.count; // Not working
}
答案 0 :(得分:1)
(res.snippet as JObject).Count
答案 1 :(得分:0)
aspark解决方案的语法略有不同,
private static void Main(string[] args)
{
string json = @"
{
a: 1,
b: 2,
c: [
{c1: 'asdas', c2:231},
{c1: 'aaaaaa', c2: 100},
{c1: 'x', c2: 83823}
]
}
";
Console.WriteLine(JObject.Parse(json)["c"].Count());
//following also works
Console.WriteLine(o.Property("c").Value.Count());
//both print 3
}