从c#4.0中的Json中检索特定值

时间:2012-12-14 15:58:02

标签: c# asp.net json

我必须从json中检索特定值。例如:字符串格式中的“id”:“217331368373746”。 下面是json查询,我的代码为acess id = 217331368373746

{
   "id": "191746304265586",
   "albums": {
      "data": [
         {
            "id": "217331368373746",
            "created_time": "2012-03-24T20:53:34+0000"
         }
      ],
      "paging": {
         "next": "https://graph.facebook.com/191746304265586/albums?limit=1&fields=id&after=MjE3MzMxMzY4MzczNzQ2"
      }
   }
}

CODE:

var jsonalbum = Retrivedata("https://graph.facebook.com/191746304265586?fields=albums.limit(1).fields(id)");
        foreach (var objalbum in jsonalbum["albums"])
        {
            //here what code
        }

    public dynamic Retrivedata(string query)
    {
        var request = WebRequest.Create(query);
        var twitpicResponse = (HttpWebResponse)request.GetResponse();
        var reader = new StreamReader(twitpicResponse.GetResponseStream());
        JavaScriptSerializer js = new JavaScriptSerializer();
        return js.Deserialize<dynamic>(reader.ReadToEnd());
    }

2 个答案:

答案 0 :(得分:1)

看起来像

foreach (var objalbum in jsonalbum["albums"]["data"])
{
   var id = objalbum["id"]
}

答案 1 :(得分:0)

您的data列表中似乎还有albums属性(数组),所以您必须这样做:

foreach (var objalbum in jsonalbum["albums"]["data"])
{
   var id = objalbum["id"];
   ...
}