0

我正在对 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));

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

4

1 回答 1

0

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

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

于 2013-06-09T06:46:27.373 回答