如何在循环中获取数据部分以获取c#json.net中此json数据格式的id名称和类型
{
"reactions":{
"data":[
{
"id":"201852446499743",
"name":"Kalna@kalna",
"type":"LIKE"
},
{
"id":"178345282632225",
"name":"Riya Modak",
"type":"LOVE"
}
],
"paging":{
"cursors":{
"before":"TWpBeE9EVXlORFEyTkRrNU56UXpPakUwT0RFeE1UVXhOekE2TWpVME1EazJNVFl4TXc9PQZDZD",
"after":"TVRBd01ERXpOekF5TmpJek56UXhPakUwT0RFeE1UVXhNems2TnpnNE5qUTRNRE0zT1RFek16RXkZD"
}
}
},
"id":"1402884576396518"
}
答案 0 :(得分:3)
快速的方法是deserialize进入dynamic类,如下所示:
// Fetch your JSON
string json = // however you retrieve this JSON
// Use JSON.NET to deserialize into dynamic
var result = JsonConvert.DeserializeObject<dynamic>(json);
// Loop through each "reaction"
foreach (var data in result.reactions.data)
{
// Show ID (can get name and type also)
Console.WriteLine(data.id.ToString());
}
输出:
201852446499743
178345282632225
答案 1 :(得分:1)
您只需导入System.Web.Script.Serialization
- 命名空间。然后,您可以使用JavaScriptSerializer
反序列化您的JSON字符串:
Dictionary<object, object> data = new JavaScriptSerializer().Deserialize<Dictionary<object, object>>(yourJSONString);
这将返回一个字典,您可以在其中找到键&#34;数据&#34;。