以下JSON数据来自php,
我试过以下代码。但控制台显示undefined
错误。
jQuery.each(result.address, function(obj) {
console.log(obj.city);
});
编辑:它也没有工作并抛出未定义的错误。
jQuery.each(result.address, function(obj) {
console.log(obj[0].city);
});
工作正常:console.log(result.address.address1.city);
。但在这种情况下,address1不是固定的。例如。 result.address.xyz.city, result.address.abc.city
答案 0 :(得分:5)
答案 1 :(得分:0)
也许这样:
for (i = 0; i < result.address.length; i++) {
console.log(result.address[i].city);
}
我不确定jquery.each是否适用于这种情况。
我希望它有所帮助。
答案 2 :(得分:0)
Javascript
var dictionary = {
"12Jan2013": [{
"id": "0",
"name": "ABC"
}, {
"id": "1",
"name": "DEF"
}],
"13Jan2013": [{
"id": "0",
"name": "PQR"
}, {
"id": "1",
"name": "xyz"
}]
};
for (var i in dictionary) {
dictionary[i].forEach(function(elem, index) {
console.log(elem, index);
});
}
输出
Object {id: "0", name: "ABC"} 0
Object {id: "1", name: "DEF"} 1
Object {id: "0", name: "PQR"} 0
Object {id: "1", name: "xyz"} 1