如何在循环中获取数据部分以获取ID名称并从C#json.net中的此JSON数据格式键入
{
"reactions":{
"data":[
{
"id":"201852446499743",
"name":"Kalna@kalna",
"type":"LIKE"
},
{
"id":"178345282632225",
"name":"Riya Modak",
"type":"LOVE"
}
],
"paging":{
"cursors":{
"before":"TWpBeE9EVXlORFEyTkRrNU56UXpPakUwT0RFeE1UVXhOekE2TWpVME1EazJNVFl4TXc9PQZDZD",
"after":"TVRBd01ERXpOekF5TmpJek56UXhPakUwT0RFeE1UVXhNems2TnpnNE5qUTRNRE0zT1RFek16RXkZD"
}
}
},
"id":"1402884576396518"
}
一种快速的方法是这样的方法:
:// 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
17834528263225
您只需要导入 System.Web.Script.Serialization
-namespace即可。然后,您可以使用JavaScriptSerializer
对您的JSON弦进行估算:
Dictionary<object, object> data = new JavaScriptSerializer().Deserialize<Dictionary<object, object>>(yourJSONString);
这是返回词典,您将在其中找到密钥"数据"。