我有困難從新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