我有困难从新Facebook的JS SDK新的图形API访问返回的JSON响应数据调用。从FB.api的响应访问数据()
例如,在他们的一些文档,他们使用的是使用SDK的老办法,他们通过反应得到的指针数据[0]
但在这里,它表明你需要使用response.data [0]改为:http://developers.facebook.com/tools/console/(点击fb.api - 相册)
那么这是哪一个呢?我知道用我的代码下面,如果我尝试使用response [0]类型的语法来获取返回的JSON,我得到未定义。
如果我使用响应[0]。长度我也得到了一个未定义
但如果我尝试response.data [0]。长度我得到2我的猜测是返回JSON或我的2 albums..I只是不知道如何玩这个返回的对象的语法和操纵它,它的属性等。
我想最终解析出返回的JSON使用jQuery parseJSON
方法,但不知道如何甚至在响应中传递正确的语法,并使用该响应对象。
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);
}
这个警报给了我2这是有道理的。我有2张专辑。
然后我尝试了类似response.data [0]的东西,并尝试了jQuery parseJSON(response.data)
或parseJSON(response.data[0])
,并且它不起作用。那么有人可以在这里解释这个响应对象吗?我没有看到关于如何使用那个返回的对象以及它是如何构造的文档。
更新:
好了,下面是我到目前为止已经灭掉整个分析方法的尝试。我不知道jQuery解析是否是100%的好代码,但我有点困惑,但我甚至无法测试,直到我找出如何使用这个响应对象回来。我知道它正在返回JSON,因为我在JS SDK中的另一个方法中解析了另一个Facebook响应对象,所以非常肯定响应[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;
}
是的,尝试过,但也没有定义。我想解析出返回的JSON字符串。他们确实发回JSON我想......非常确定。 – PositiveGuy 2010-07-26 15:34:53
@coffeeaddict:所以'alert(response.data [0] .name);'什么也不做?我没有看到你手动解析的例子,它甚至在你调用你的回调函数之前由API库处理。 – 2010-07-26 15:43:23
实际上这是我试过的名字,因为每个专辑都有一个名称属性。但让我再试一次。我会发布我的解析..它发生在我上面的例子中的警报后,但因为我得到未定义,因为可能我不知道我回来或如何使用该对象,我的解析失败以及与未定义。 – PositiveGuy 2010-07-26 15:55:02