我有一個JSON,我需要得到這個JSON,並將其放入html中作爲ul列表。它獲取對象的值並在html中顯示[object Object]。如果我修改json然後它的作品。所以在我的腳本中可能有些問題,我無法正確循環json文件。能有人幫助請:getJSON顯示[對象對象]而不是實際值
我的JSON是:
[
{
"us":"USA"
},
{
"fr":"FRANCE"
},
{
"es":"Spain"
},
{
"sa":"South Africa"
}
]
和JS IS
<script>
$.getJSON('jsonfile', function(data) {
var items = [];
$.each(data ,function(key,val) {
items.push('<li id="'+ key +'">' + val +'</li>');
});
$('<ul />' , {
'class':'new-div',
html:items.join('')
}).appendTo('body');
});
</script>
修訂JSON:
[
{
"items":
{
"item":
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}
]
}
}
]
你能說出你想要的結果嗎? 「迭代」有點太模糊 - 屏幕上應顯示哪些元素,以及它們包含的JSON數據是什麼? – pimvdb
@pimvdb:我需要這個json來解析我的方法:http://adobe.github.com/Spry/samples/data_region/JSONDataSetSample.html#Example10。輸出應該是如下圖所示的鏈接 – Mike
的示例10看看http://stackoverflow.com/questions/11922383/i-have-a-nested-data-structure-json-how-can-access -a特定的價值。如果您知道JSON的結構,則訪問正確的值不應該很困難。 –