从FB.api()的响应中访问数据

Pos*_*Guy 5 facebook

我无法从新的Facebook JS SDK 新的Graph API调用中访问返回的JSON响应数据.

例如,在他们使用旧方法使用SDK的一些文档中,他们通过响应[0]获得指向数据的指针.

但是在这里,它显示你需要使用response.data [0]:http://developers.facebook.com/tools/console/(点击fb.api - photo-albums)

那是哪个呢?我知道,使用下面的代码,如果我尝试使用response [0]类型的语法来获取返回的JSON,我会得到未定义的.

如果我使用response [0] .length,我也会被定义

但是,如果我尝试response.data [0] .length我得到2,我猜是返回的JSON或我的2张专辑..我只是不知道如何使用这个返回的对象在语法和操作它,它的财产等

我想最终使用jQuery parseJSON方法解析返回的JSON,但是不知道如何在这里为响应传递正确的语法并且只使用该响应对象.

FB.api(uri, function(response)
{
    alert("response: " + response);

    // check for a valid response
    if (response == "undefined" || response == null || !response || response.error)
    {
        alert("error occured");
        return;
    }

    alert("response length: " + response.data.length);
}
Run Code Online (Sandbox Code Playgroud)

这个警报给了我2这是有道理的.我有2张专辑.

然后我尝试了像response.data [0]之类的东西,尝试了一个jQuery parseJSON(response.data)或者parseJSON(response.data[0])它并没有用.那么有人可以在这里解释响应对象,就像Facebook一样吗?我看不到关于如何使用返回的对象以及它是如何构造的文档.

更新:

好的,所以这是我到目前为止已完成的整个解析方法尝试.我不知道jQuery解析是否是100%好的代码,我有点抄袭但我甚至无法测试,直到我弄清楚如何使用这个响应对象回来.我知道它正在返回JSON,因为我在JS SDK中的另一个方法中解析了另一个facebook响应对象,因此非常确定response [0]或response.data [0]将为您提供JSON字符串.

function GetAllFacebookAlbums(userID, accessToken)
{
    alert("inside GetAllFacebookAlbums(userID, acessToken)");

    var dFacebookAlbums = {}; // dictionary
    var uri = "/" + userID + "/albums?access_token=" + accessToken;
    //var uri = "/me/albums";

    alert("get albums uri: " + uri);

    FB.api(uri, function(response) 
    {
        alert("response: " + response);

        // check for a valid response
        if (response == "undefined" || response == null || !response || response.error) 
        {
            alert("error occured");
            return;
        }

        alert("response length: " + response.data.length);

        for (var i=0, l=response.data.length; i<l; i++)
        {
            alert("response[key]: " + response.data[i].Name);
        }

        // parse JSON from response
        var dJSONObjects = jQuery.parseJSON(response.data);

        alert("dJSONObjects: " + dJSONObjects);

        if (dJSONObjects.length == 0)
            return;

        // iterate through the dictionary of returned JSON objects 
        // and transform each to a custom facebookAlbum object
        // then add each new FacebookAlbum to the final dictionary 
        // that will return a set of facebookAlbums
        $.each(json.attributes, function () {
            // add a new album to the dictionary
            aFacebookAlbums[i] = FacebookAlbum(
                                                jsonObject["id"],
                                                jsonObject["name"],
                                                jsonObject["location"],
                                                jsonObject["count"],
                                                jsonObject["link"]
                                              );
        });
    });

    return dFacebookAlbums;
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*nen 0

我没有看过 API,但我怀疑 FB 会给你数组中的 JSON 编码字符串。您是否尝试过仅访问response.data[0].someproperty?