我有一些 json 代码,其中包含多个对象,例如:
[
{
"MNGR_NAME": "Mark",
"MGR_ID": "M44",
"EMP_ID": "1849"
},
{
"PROJ_ID": "88421",
"PROJ_NAME": "ABC",
"PROJ_ALLOC_NO": "49"
}
]
我的 JSON 循环片段是
function ServiceSucceeded(result)
{
for(var x=0; x<result.length; x++)
{
alert(result[i].MNGR_NAME);
alert(result[i].MGR_ID);
alert(result[i].EMP_ID);
alert(result[i].PROJ_ID);
alert(result[i].PROJ_NAME);
alert(result[i].PROJ_ALLOC_NO);
}
}
当我实现它时,它会显示提示 undefined
since result[0]
keys != result[1]
keys。
例如:result[0].MNGR_NAME
(第一个数组)给你“Mark”
但是result[1].MNGR_NAME
(第二个Array) 根本不在数组中,因此给你 undefined
请问您能告诉我地址吗?我不应该得到 undefined
。
最佳答案
我会用
for(index in result) {
var obj = result[index];
for(objectIndex in obj) {
if(objectIndex != "PROJ_ALLOC_NO") {
// Only alert if the key of the object is not PROJ_ALLOC_NO
alert(objectIndex + ": " + obj[objectIndex]);
}
}
}
这遍历数组,然后遍历对象并显示对象的每个键和值!
关于javascript - 如何循环遍历多个对象的json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8412098/