$.getJSON 和 $.each
所以我试图循环遍历 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 列表中。我没有正确调用其中一个变量吗?
So i'm trying to cycle through a JSON response, but I can't seem to get it right.
Snippet:
$.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"
}
}
]);
It just doesn't seem to go down the JSON list. Did I not call one of the variables correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是无效的 JSON。您需要引用您的密钥:
..等等。
未来项目的一个方便参考是 JSONLint JSON 验证器
This is invalid JSON. You need to quote your keys:
.. and so on.
A handy reference for future projects is the JSONLint JSON Validator
首先,您向我们展示的 JSON...并不完全是 JSON。我不确定 jQuery.getJSON 是否可以读取它。
其次,我不知道
self
是做什么的。我认为它指的是最外层的范围,对吗?第三,要求正确缩进和可运行的代码是否太过分了?:
顺便说一句,我会执行console.log(playlists,self,this)来确保一切正常。
First of, the JSON you're showing us....isn't exactly JSON. I'm not sure if jQuery.getJSON can read that.
Secondly, I don't know what
self
does. I think it refers to the outermost scope, right?Thirdly, is it too much to ask for properly indented and runnable code?:
By the way, I'd do
console.log(playlists,self,this)
to ensure everything is ok.