首先,您必须修复您的对象文字。您必须使用: not =作为键值对。
之后,您可以通过以下方式进行迭代:
var obj = [ {"id":"1", "result":[{"name":"Sam","age":"12"},{"sport":"soccer"}]},
{"id":"2", "result":[{"name":"Paul","age":"43"},{"sport":"basketball"}]}];
for (var i = 0; i < obj.length; i += 1) {
console.log("Name", obj[i].result[0].name);
console.log("Age", obj[i].result[0].age);
console.log("Sport", obj[i].result[1].sport);
}
如果您想使用循环进行整个遍历,您可以使用:
var obj = [ {"id":"1", "result":[{"name":"Sam","age":"12"},{"sport":"soccer"}]},
{"id":"2", "result":[{"name":"Paul","age":"43"},{"sport":"basketball"}]}];
for (var i = 0; i < obj.length; i += 1) {
for (var j = 0; j < obj[i].result.length; j += 1) {
var current = obj[i].result[j];
for (var prop in current) {
console.log(prop, current[prop]);
}
}
}