任何人都可以提出一种方法来在javascript中使用jQuery来迭代格式的json数组。迭代json数组
{"0",{"id":"10", "class": "child-of-9"},"1",{"id":"11", "class": "child-of-10"}}
我需要得到每个类的价值。
任何人都可以提出一种方法来在javascript中使用jQuery来迭代格式的json数组。迭代json数组
{"0",{"id":"10", "class": "child-of-9"},"1",{"id":"11", "class": "child-of-10"}}
我需要得到每个类的价值。
没有点有一个对象,其中键是数字。这是数组如何处理索引。你在那里有一个对象,而不是阵列。
打开本:
a = {"0",{"id":"10", "class": "child-of-9"},"1",{"id":"11", "class": "child-of-10"}}
进入这个:
a = [{"id":"10", "class": "child-of-9"},{"id":"11", "class": "child-of-10"}]
而且你有一个数组对象的。这可以用一个简单的for循环迭代,并通过索引访问。
for(var i=0; i<a.length; i++) {
console.log(a[i]);
}
你json不正确。它应该看起来更像
{"0": {"id":"10", "class": "child-of-9"},"1": {"id":"11", "class": "child-of-10"}};
一旦它确实可以使用jQuery.each()来遍历它。
var data = {"0": {"id":"10", "class": "child-of-9"},"1": {"id":"11", "class": "child-of-10"}};
jQuery.each(data, function(i,a){
console.log(a['class']);
});
顺便说你的JSON是不是一个数组是一个对象。要构建阵列版本,它看起来像
[{"id":"10", "class": "child-of-9"},{"id":"11", "class": "child-of-10"}]