非常基本的JSON问题 [英] Very basic JSON question
本文介绍了非常基本的JSON问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下JSON数据:
I have this JSON data:
var people = [
{ name : "John", age : 25 },
{ name : "Jane", age : 49 },
{ name : "Jim", age : 31 },
{ name : "Julie", age : 39 },
{ name : "Joe", age : 19 },
{ name : "Jack", age : 48 }
];
我该如何遍历人体内的所有对象,并像这样输出他们的名字和年龄:
How can I loop through all the objects inside people and output their names along with their ages like so:
John 25
Jame 49
followed by the rest...
推荐答案
使用for循环: http ://www.w3schools.com/js/js_loop_for.asp
for (var i = 0; i < people.length; i++)
{
document.write(people[i].name + ' ' + people[i].age + '<br />');
}
或jQuery中的each
函数: http://api.jquery.com/jQuery.每个/
or the each
function in jQuery: http://api.jquery.com/jQuery.each/
$.each(people, function(i, o) {
document.write(o.name + ' ' + o.age + '<br />');
});
这篇关于非常基本的JSON问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文