在下面找到 json 响应...
{"Object":
{"PET ANIMALS":[{"id":1,"name":"Dog"},
{"id":2,"name":"Cat"}],
"Wild Animals":[{"id":1,"name":"Tiger"},
{"id":2,"name":"Lion"}]
}
}
在上面提到的响应中,如何找到“PET ANIMALS”和“Wild ANIMALS”的长度......
最佳答案
var json = /* your JSON object */ ;
json["Object"]["PET ANIMALS"].length // returns the number of array items
使用循环打印数组中的项目数:
var obj = json["Object"];
for (var o in obj) {
if (obj.hasOwnProperty(o)) {
alert("'" + o + "' has " + obj[o].length + " items.");
}
}
关于javascript - 如何访问名称中有空格的 Json 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4042646/