所以我试图循环一个JSON响应,但我似乎无法做到正确。
段:
$.getJSON("/playlist/",function(playlists) {
$.each(playlists,function() {
self.playlists[this.playlist.id] = new SC.Playlist(this, self);
console.log(this);
})
})
JSON:
jsonp1311444173992([
{
is_owner: true,
id: "wtf",
playlist: {
id: "latest1",
name: "Hot Tracks1",
version: 0,
tracks: "33+44+55"
}
},
{
is_owner: true,
playlist: {
id: "latest",
name: "Hot Tracks",
smart: true,
version: 0,
smart_filter: {
order: "hotness"
}
}
},
{
is_owner: true,
playlist: {
id: "latest3",
name: "Hot Tracks3",
version: 0,
tracks: "33+44+55"
}
},
{
is_owner: true,
playlist: {
id: "latest4",
name: "Hot Tracks4",
version: 0,
tracks: "33+44+55"
}
},
{
is_owner: true,
playlist: {
id: "latest5",
name: "Hot Tracks5",
version: 0,
tracks: "33+44+55"
}
}
]);
它似乎没有沿着JSON列表。我没有正确地调用其中一个变量吗?
答案 0 :(得分:5)
这是无效的JSON。你需要引用你的钥匙:
{
"is_owner": true,
"id": "wtf",
"playlist": {
"id": "latest1",
"name": "Hot Tracks1",
"version": 0,
"tracks": "33+44+55"
}
},
..等等。
未来项目的便捷参考是JSONLint JSON Validator
答案 1 :(得分:0)
首先,你向我们展示的JSON ......并不完全是JSON。我不确定jQuery.getJSON是否可以读取它。
其次,我不知道self
的作用。我认为它指的是最外层的范围,对吧?
第三,要求正确缩进和可运行的代码是否太过分了?:
$.getJSON("/playlist/",function(playlists) {
$.each(playlists,function() {
self.playlists[this.playlist.id] = new SC.Playlist(this, self);
console.log(this);
}
}
顺便说一句,我会console.log(playlists,self,this)
确保一切正常。