0
我有這樣讀嵌套對象
var JSON_Object = {
"Diabites": [
{
"Day1": [
{
"Breakfast": [
{
"food1": "Bread",
"food2": "Tea",
"food3": "Edd"
}
]
},
{
"Lunch": [
{
"food1": "Bread",
"food2": "Tea",
"food3": "Edd"
}
]
},
{
"Dinner": [
{
"food1": "Bread",
"food2": "Tea",
"food3": "Edd"
}
]
}
]
},
{
"Day2": [
{
"Breakfast": [
{
"food1": "Bread",
"food2": "Tea",
"food3": "Edd"
}
]
},
{
"Lunch": [
{
"food1": "Bread",
"food2": "Tea",
"food3": "Edd"
}
]
},
{
"Dinner": [
{
"food1": "Bread",
"food2": "Tea",
"food3": "Edd"
}
]
}
]
}
]
}
對象現在,我怎麼能使用一些循環打印出來閱讀本文件。我嘗試以下
alert(JSON_Object.Diabites.length); // Return 1
alert(JSON_Object.Diabites[0].Day1.length); // Return 3
但也有不同的名稱,如Day1, Day2, Day3 ....
,所以我怎麼能更改部分的循環,就像喜歡
for(var i=0; i<JSON_Object.Diabites.length;i++)
{
alert(JSON_Object.Diabites[i].Day1.length);
}
更新JSON對象
var JSON_Object = {
"Diabites": [
{
"Day1": [
{
"Breakfast": [
{
"food1": "Bread"
},
{
"food2": "Tea"
},
{
"food3": "Edd"
}
]
},
{
"Lunch": [
{
"food1": "Bread"
},
{
"food2": "Tea"
},
{
"food3": "Edd"
}
]
},
{
"Dinner": [
{
"food1": "Bread"
},
{
"food2": "Tea"
},
{
"food3": "Edd"
}
]
}
]
},
{
"Day2": [
{
"Breakfast": [
{
"food1": "Bread"
},
{
"food2": "Tea"
},
{
"food3": "Edd"
}
]
},
{
"Lunch": [
{
"food1": "Bread"
},
{
"food2": "Tea"
},
{
"food3": "Edd"
}
]
},
{
"Dinner": [
{
"food1": "Bread"
},
{
"food2": "Tea"
},
{
"food3": "Edd"
}
]
}
]
}
]
};
[訪問/進程(嵌套)對象,數組或JSON的可能重複](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json)和[如何做我列舉了一個JavaScript對象的屬性?](http://stackoverflow.com/q/85992/218196)。 –
請注意,該問題有**沒有**與JSON完全相關。這是關於如何在JavaScript中處理數組/對象。 *您如何獲得數據(例如,通過JSON)與問題無關。 –