0

我必须从 json 中检索特定值。例如:字符串格式的“id”:“217331368373746”。下面是 json 查询和我访问 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"
      }
   }
}

代码:

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());
    }
4

2 回答 2

1

好像

foreach (var objalbum in jsonalbum["albums"]["data"])
{
   var id = objalbum["id"]
}
于 2012-12-14T16:01:48.270 回答
0

看起来data您的列表中也有一个属性(数组),albums所以您必须这样做:

foreach (var objalbum in jsonalbum["albums"]["data"])
{
   var id = objalbum["id"];
   ...
}
于 2012-12-14T16:00:19.407 回答