$.getJSON 和 $.each

发布于 2024-11-26 06:15:24 字数 1144 浏览 1 评论 0原文

所以我试图循环遍历 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦情居士 2024-12-03 06:15:24

这是无效的 JSON。您需要引用您的密钥:

{
    "is_owner": true,
    "id": "wtf",
    "playlist": {
      "id": "latest1",
      "name": "Hot Tracks1",
      "version": 0,
      "tracks": "33+44+55"
    }
  },

..等等。

未来项目的一个方便参考是 JSONLint JSON 验证器

This is invalid JSON. You need to quote your keys:

{
    "is_owner": true,
    "id": "wtf",
    "playlist": {
      "id": "latest1",
      "name": "Hot Tracks1",
      "version": 0,
      "tracks": "33+44+55"
    }
  },

.. and so on.

A handy reference for future projects is the JSONLint JSON Validator

泼猴你往哪里跑 2024-12-03 06:15:24

首先,您向我们展示的 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)来确保一切正常。

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?:

$.getJSON("/playlist/",function(playlists) {
    $.each(playlists,function() {
        self.playlists[this.playlist.id] = new SC.Playlist(this, self);
        console.log(this);
    }
}

By the way, I'd do console.log(playlists,self,this) to ensure everything is ok.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文