在 C# 中迭代 JSON 对象 [英] Iterating over JSON object in C#
本文介绍了在 C# 中迭代 JSON 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 C# 中使用 JSON.NET 来解析来自 Klout API 的响应.我的回答是这样的:
<预><代码>[{"id": "5241585099662481339","displayName": "音乐","name": "音乐","slug": "音乐","imageUrl": "http://kcdn3.klout.com/static/images/music-1333561300502.png"},{"id": "6953585193220490118","displayName": "名人","name": "名人","slug": "名人","imageUrl": "http://kcdn3.klout.com/static/images/topics/celebrities_b32741b6703151cc7bd85fba24c44c52.png"},{"id": "5757029936226020304","displayName": "娱乐","name": "娱乐","slug": "娱乐","imageUrl": "http://kcdn3.klout.com/static/images/topics/Entertainment_7002e5d2316e85a2ff004fafa017ff44.png"},{"id": "3718","displayName": "周六夜现场","name": "周六夜现场","slug": "周六夜现场","imageUrl": "http://kcdn3.klout.com/static/images/icons/generic-topic.png"},{"id": "8113008320053776960","displayName": "好莱坞","name": "好莱坞","slug": "好莱坞","imageUrl": "http://kcdn3.klout.com/static/images/topics/hollywood_9eccd1f7f83f067cb9aa2b491cd461f3.png"}]如您所见,它包含 5 个 id
标签.也许下次它会是 6 或 1 或其他一些数字.我想遍历 JSON 并获取每个 id
标记的值.我不能在不知道会有多少循环的情况下运行循环.我该如何解决这个问题?
解决方案
dynamic dynJson = JsonConvert.DeserializeObject(json);foreach(dynJson 中的 var 项目){Console.WriteLine("{0} {1} {2} {3}
", item.id, item.displayName,item.slug, item.imageUrl);}
或
var list = JsonConvert.DeserializeObject>(json);公共类 MyItem{公共字符串ID;公共字符串显示名称;公共字符串名称;公共字符串弹头;公共字符串 imageUrl;}
I am using JSON.NET in C# to parse a response from the Klout API. My response is like this:
[
{
"id": "5241585099662481339",
"displayName": "Music",
"name": "music",
"slug": "music",
"imageUrl": "http://kcdn3.klout.com/static/images/music-1333561300502.png"
},
{
"id": "6953585193220490118",
"displayName": "Celebrities",
"name": "celebrities",
"slug": "celebrities",
"imageUrl": "http://kcdn3.klout.com/static/images/topics/celebrities_b32741b6703151cc7bd85fba24c44c52.png"
},
{
"id": "5757029936226020304",
"displayName": "Entertainment",
"name": "entertainment",
"slug": "entertainment",
"imageUrl": "http://kcdn3.klout.com/static/images/topics/Entertainment_7002e5d2316e85a2ff004fafa017ff44.png"
},
{
"id": "3718",
"displayName": "Saturday Night Live",
"name": "saturday night live",
"slug": "saturday-night-live",
"imageUrl": "http://kcdn3.klout.com/static/images/icons/generic-topic.png"
},
{
"id": "8113008320053776960",
"displayName": "Hollywood",
"name": "hollywood",
"slug": "hollywood",
"imageUrl": "http://kcdn3.klout.com/static/images/topics/hollywood_9eccd1f7f83f067cb9aa2b491cd461f3.png"
}
]
As you see, it contains 5 id
tags. Maybe next time it would be 6 or 1 or some other number. I want to iterate over the JSON and get the value of each id
tag. I can't run a loop without knowing how many there will be. How can I solve this?
解决方案
dynamic dynJson = JsonConvert.DeserializeObject(json);
foreach (var item in dynJson)
{
Console.WriteLine("{0} {1} {2} {3}
", item.id, item.displayName,
item.slug, item.imageUrl);
}
or
var list = JsonConvert.DeserializeObject<List<MyItem>>(json);
public class MyItem
{
public string id;
public string displayName;
public string name;
public string slug;
public string imageUrl;
}
这篇关于在 C# 中迭代 JSON 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文