解析JSON时遇到问题,从Facebook Request,javascript返回

时间:2013-06-09 06:43:47

标签: javascript json facebook-graph-api

我正在对facebook API进行休息调用。当我按以下方式处理响应时。

$("#fb-root").append(JSON.stringify(response));

我收到了字符串:

[{
        "code": 200,
        "headers": [{
                "name": "Access-Control-Allow-Origin",
                "value": "*"
            }, {
                "name": "Cache-Control",
                "value": "private, no-cache, no-store, must-revalidate"
            }, {
                "name": "Connection",
                "value": "close"
            }, {
                "name": "Content-Type",
                "value": "text/javascript; charset=UTF-8"
            }, {
                "name": "ETag",
              "value": "\"{e_tag}\""
            }, {
                "name": "Expires",
                "value": "Sat, 01 Jan 2000 00:00:00 GMT"
            }, {
                "name": "Pragma",
                "value": "no-cache"
            }
        ],
  "body": "{\n \"data\": [\n {\n \"name\": \"{name}\",\n \"id\": \"{id}\"\n },\n {\n \"name\": \"{name}\",\n \"id\": \"{id}\"\n },\n {\n \"name\": \"{name}\",\n \"id\": \"{uid}\"\n },\n {\n \"name\": \"{name}\",\n \"id\": \"{uid}\"\n }\n ],\n \"paging\": {\n \"next\": \"http://graph.facebook.com/{my_uid}/mutualfriends?user={uid_1}&access_token={access_token}&limit=5000&offset=5000&__after_id={last_id}\"\n }\n}"
    }
]

该行:

$("#fb-root").append(JSON.stringify(response[0].body));

返回:

"{\n \"data\": [\n {\n \"name\": \"Dan Weiss\",\n \"id\": \"11700083\"\n },\n {\n \"name\": \"Sonny Pearce\",\n \"id\": \"613521868\"\n },\n {\n \"name\": \"Cody Frost\",\n \"id\": \"678251338\"\n },\n {\n \"name\": \"Nathaniel Brown\",\n \"id\": \"1359093168\"\n }\n ],\n \"paging\": {\n \"next\": \"http://graph.facebook.com/554718794/mutualfriends?user=517472645&access_token=CAAFSqbUvb0IBAHPL4pfwTiBcc2EN00Kal6QWiNW1P0I1HDBqFkZCh8uDD6UWJkRRfam6GtlAtynWRp5eZBweDCWaJlkVMH7DSkwhgAXbQwvbOhEi0ZA2XDyyuOI7oq5yKT0zH9N6ZACrAMMeWInJOjwrrHBZBsewZD&limit=5000&offset=5000&__after_id=1359093168\"\n }\n}"

但是

$("#fb-root").append(JSON.stringify(response[0].body.data));

返回空白页面。谁能解释我做错了什么?

1 个答案:

答案 0 :(得分:0)

response[0].body.data是一个字符串。所以你需要先转换为JSON才能使用。

$("#fb-root").append(JSON.stringify((JSON.parse(response[0].body)).data));