我有以下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如何做到这一点? 提前谢谢。
答案 0 :(得分:8)
非常简单,请使用jQuery.each
:
$.each(data, function (index, item) {
console.log(item);
});
但是,你真的不需要jQuery来完成这个简单的任务,试试本地Array.prototype.forEach
:
data.forEach(function (item) {
console.log(item);
});
如果你必须支持旧的浏览器并且不想依赖于某个库,那么for
- 循环可以解决问题:
for (var i = 0; i < data.length; ++i) {
var item = data[i];
}
答案 1 :(得分: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>