的
{}在JSON表示一个对象。对象的每个属性均以
key:value逗号分隔。像这样,可以通过使用句点运算符的键来访问属性值
json.forum。的
[]在JSON表示一个数组。数组值可以是任何对象,并且值以逗号分隔。要遍历数组,请使用带有索引的标准for循环。要遍历对象的属性而不直接通过键引用它们,可以使用
forin循环:
var json = {"forum":[{"id":"1","created":"2010-03-19 ","updated":"2010-03-19 ","user_id":"1","vanity":"gamers","displayname":"gamers","private":"0","description":"All things gaming","count_followers":"62","count_members":"0","count_messages":"5","count_badges":"0","top_badges":"","category_id":"5","logo":"gamers.jpeg","theme_id":"1"}]};var forum = json.forum;for (var i = 0; i < forum.length; i++) { var object = forum[i]; for (property in object) { var value = object[property]; alert(property + "=" + value); // This alerts "id=1", "created=2010-03-19", etc.. }}
如果您想以jQueryish的方式进行 *** 作,请抓住
$.each():
$.each(json.forum, function(i, object) { $.each(object, function(property, value) { alert(property + "=" + value); });});
我使用了与“普通Javascript”方式相同的变量名,以便您更好地了解jQuery在其“幕后”所做的工作。希望这可以帮助。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)