我有一个JSON对象musicerobj,下面粘贴了一个示例:
{
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.")
}
}
);}
最佳答案
你解码了吗?看来它仍然是一个字符串。
musicianobj = JSON.parse(musicianobj);
console.log(musicianobj.name); // Now this should work
关于javascript - 无法访问JSON对象的子级,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13535200/