4

我有以下 json 数据

[
  {
    id: "79",
    title: "Web+Infographics",
    path: "web-infographics"
  },
  {
    id: "80",
    title: "Miscellaneous",
    path: "miscellaneous"
  },
  {
    id: "81",
    title: "Entertainment",
    path: "entertainment"
  }
]

我想使用 jquery 从中获取 id、title 和 path 我该怎么做?提前致谢。

4

2 回答 2

11

很简单,使用jQuery.each

$.each(data, function (index, item) {
  console.log(item);
});

但是,你真的不需要 jQuery 来完成这个简单的任务,Array.prototype.forEach试试原生的:

data.forEach(function (item) {
  console.log(item);
});

如果您必须支持较旧的浏览器并且不想依赖库,for则可以使用 -loop 来解决问题:

for (var i = 0; i < data.length; ++i) {
  var item = data[i];
}
于 2013-07-30T13:05:59.330 回答
4
<script>

var data = [
  {
    id: "79",
    title: "Web+Infographics",
    path: "web-infographics"
  },
  {
    id: "80",
    title: "Miscellaneous",
    path: "miscellaneous"
  },
  {
    id: "81",
    title: "Entertainment",
    path: "entertainment"
  }
];

$.each(data, function(key, value) {
    alert(value.id + ", " + value.title + ", " + value.path);
});

</script>
于 2013-07-30T13:07:25.733 回答