开发者

Facebook FB.api - just use the JSON object it sends back?

开发者 https://www.devze.com 2023-01-08 13:17 出处:网络
So taking a look at the API doc page here: http://developers.facebook.com/docs/reference/javascript/FB.api

So taking a look at the API doc page here:

http://developers.facebook.com/docs/reference/javascript/FB.api

I'm wondering if the response received back is an HttpResponse or a JSON object. They say that they return a JSON object in the response.

So since they are performing things such as response.name, etc. does it mean we don't need to parse the JSON object? I don't get it. 开发者_JAVA技巧 I was going to use the jQuery parseJSON to parse the returned JSON object so I could traverse through it and grab the data.

UPDATED:

Ok well here's my actual code:

var uri = "/" + userID + "/albums";

    FB.api(uri, function (response) {
        // check for a valid response
        if (!response || response.error)
        {
            alert("error occured");
            return;
        }


        alert("console.log(response): " + console.log(response));
        alert("response: " + response[0].length);
});

the uri being passed in is this: /1637262814/albums


So they are just returning a JSON string, but your programming language wraps that into an HttpResponse. you have to extract the JSON string from the response and then parse it.


You get a JavaScript value back. Graph API always returns an object, but some old methods return numbers or booleans. Usually this is an object like { name: 'My Name', id: 1234 }. Easiest to run this in firebug: FB.api('/me', function(r) { console.log(r) }) as it will let you explore the response. You can also take a look at this example: http://fbrell.com/fb.api/user-info.


I don't mean to be impolite, but I think the fastest way to find this out would be to just try and alert(response), and/or alert(eval(response)) and see what happens.

Just alert(response) should be enough to let you know what you're getting and how to treat it.

0

精彩评论

暂无评论...
验证码 换一张
取 消