解析Facebook的评论

时间:2011-11-22 19:06:03

标签: jquery facebook-graph-api

我正在尝试解析我从

获得的FB评论JSON
 var url = http://graph.facebook.com/comments?id={{ url_of_the_page }}

使用jQuery,我试图通过以下方式从响应中获取data.id和其他项目:

 $j.getJSON(url, function (item) {            
        console.log(item);              
    });

但是,他们在顶部粘贴了一个URL对象,所以它是:

item
 {{ url_of_the_page }}
     data 
         [0]
            id
            ...

如何访问id和其他媒体资源? item.{{ url_of_the_page }}.data不起作用..

JSON示例:

  {
 "http://www.mysiteurl.com": {
  "data": [
     {
        "id": "10150212811760417_27834638",
        "from": {
           "name": "Tomas Tomas",
           "id": "100000606151004"
        },
        "message": "test",
        "created_time": "2011-11-22T14:21:42+0000"
     },
     {
        "id": "10150212811760417_27835460",
        "from": {
           "name": "Tomas Tomas",
           "id": "100000606151004"
        },
        "message": "ThanksGiving!",
        "created_time": "2011-11-22T14:51:23+0000"
     },
     {
        "id": "10150212811760417_27835931",
        "from": {
           "name": "Tomas Tomass",
           "id": "100000606151004"
        },
        "message": "3rd comment",
        "created_time": "2011-11-22T15:08:49+0000"
     }
  ],
  "paging": {
     "next": "https://graph.facebook.com/comments?ids=http\u00253A\u00252F\u00252Fwww.mywebsite.com&limit=25&offset=25"
  }
  }
 }

3 个答案:

答案 0 :(得分:3)

试试这个

item["url_of_the_page"].data[0].id

答案 1 :(得分:0)

以下是使用for循环从JSON对象获取数据的示例:http://jsfiddle.net/jasper/4XVMp/1/

for (url in json) {
    console.log(url);
    for (index in json[url].data) {
        console.log('--> ' + index);
        for (key in json[url].data[index]) {
            console.log('---->' + key + ' = ' + json[url].data[index][key]);
        }
    }
}

答案 2 :(得分:0)

我在students.item["http://www.myurl.com"].comments.data[i].id选项上苦苦挣扎。最后让它与students["http://www.myurl.com"].comments.data[i].id

一起使用
var students = {
    "http://www.myurl.com": {
        "comments": {
            "data": [
                {
                    "id": "123456778",
                    "from": {
                        "name": "Rohit",
                        "id": "1000005"
                    },
                    "message": "Hey ROhit",
                    "can_remove": false,
                    "created_time": "2012-09-03T03:16:01+0000",
                    "like_count": 0,
                    "user_likes": false
                }
            ]
        }
    }
}