我有這樣的JSON:如何在Ajax中迭代嵌套的JSON?
{
"draw": 0,
"recordsTotal": 90,
"recordsFiltered": 41,
"data": [
{
"id": "5",
"art": "default/def2.jpg",
"full_name": " ",
"title": "Hellberg - The Girl | Atomik Remix",
"tag": "music",
"time": "2015-11-14",
"": ""
},
{
"id": "8",
"art": "default/def2.jpg",
"full_name": "Simon Deoro",
"title": "Tim McMorris-On (Single)",
"tag": "dance,popular,",
"time": "2015-11-14",
"": ""
},
...
]
}
我想回到裏面data
所有id
的,所以我想這個功能:
function getPlaylist(id) {
$.ajax({
type: "GET",
url: baseUrl+"/playlist.php?id="+id,
cache: false,
success: function(result) {
var samples = JSON.parse(result);
for (i in samples)
{
console.log(samples.data[i].id + "<br />");
}
}
});
}
但是我看到控制檯此錯誤
Uncaught TypeError: Cannot read property 'id' of undefined
我也試過這個for
循環(控制檯的語法錯誤)
for(var i = 0; i < samples.data.length; i++)
{
var product = samples.data[i];
var productId = product.id;
console.log(productId);
}
我要的是輸出5, 8
(我id
的)
我不是很熟悉JSON,所以我怎麼能訪問和遍歷我的結構是否正確?
的可能的複製[如何遍歷一個JSON結構?](http://stackoverflow.com/questions/1078118/how-do-i-iterate-over-a-json-structure) – Ryan