使用jQuery遍历JSON数据 [英] Traverse json data using jquery

查看:94
本文介绍了使用jQuery遍历JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下json数据

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

我想使用jquery从中获取ID,标题和路径,我该怎么做? 预先感谢.

and i want to get the id, title and path out of it using jquery how can i do that? Thanks in advance.

推荐答案

非常简单,请使用jQuery.each:

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

但是,您实际上不需要jQuery来完成此简单任务,请尝试使用本机Array.prototype.forEach:

But, you don't really need jQuery for this simple task, give the native Array.prototype.forEach a try:

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

如果您必须支持较旧的浏览器并且不想依赖库,则for循环可能会达到目的:

If you have to support older browsers and don't want to depend on a library, a for-loop could to the trick:

for (var i = 0; i < data.length; ++i) {
  var item = data[i];
}

这篇关于使用jQuery遍历JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆