我正在尝试遍历Javascript对象以在html页面上显示内容。内容来自已被解析为javascript对象的API。循环工作正常,但是我想出的每个解决方案要么在HTML中什么都没有给我,要么显示[object object] [object object]
我不知道为什么它会一直显示此内容或如何使其正确显示。
保存的数据来自api调用
[{"id":"68343","order_id":"77348","customer_id":"2","number":"1"},
{"id":"68344","order_id":"77348","customer_id":"2","number":"1"},
{"id":"68342","order_id":"77348","customer_id":"3","number":"1"}]
我得到数据并解析
function(data){
var result = JSON.parse(data);
if(result.status == "success")
{
//need to loop through all data so that it displays in a html div
}
}
所以我尝试了for循环:-
for (var objProp in result){
for (var obj in result[objProp]){
document.getElementById("orderItem").innerHTML = result[objProp]
[obj];
}
}
显示[对象对象]
另一个循环
for (var key in result){
document.getElementById("orderItem").innerHTML =result[key];}
可以找回一件物品
document.getElementById("orderItem").innerHTML = result.data[0].id;