的孩子我有一個JSON對象musicianobj,其中我已經粘貼下面的例子:無法訪問JSON對象
{
id: "451026389391"
name: "John Frusciante"
type: "profile"
url: "http://open.spotify.com/artist/XXXXXXXXXXXXXX"
}
我得到這個使用的JavaScript SDK Facebook的API。我可以運行console.log(musicianobj);
,它可以成功地將對象打印到Chrome中的日誌中,但是console.log(musicianobj.name);
,console.log(musicianobj[1]);
和console.log(musicianobj["name"]);
都會因沒有明顯原因返回undefined。有任何想法嗎?
編輯:下面的代碼。
var playFriendsTrack = function(friend){
FB.api("/"+friend+"/music.listens", function(data) {
var songname = data.data[0].data.song.title;
var artistname = "";
FB.api(data.data[0].data.song.id,function(trackdata){
var musicianobj = trackdata.data.musician;
console.log(musicianobj);
console.log(musicianobj["name"]); // Doesn't work
console.log(musicianobj.name); // Doesn't work
artistname = musicianobj[1]; // Doesn't work
});
if(artistname.length <= 0){
alert("Error! Please try another friend.")
}
}
);}
你是否解碼它從JSON之前戳它呢? – Zirak
嘗試:console.log(typeof musicianobj); – luin
向我們展示一些代碼! – Eric