我是jQuery的新手,我正在嘗試使用getJSON
函數。我想要做的是拉JSON文件的「id」部分,並將其推入jQuery中名爲planes
的數組中。從那裏,該數組用於自動填充功能來填充可搜索的ID。如何使用jQuery將JSON數據推送到數組中?
var planes = [];
$.getJSON('planes.json', function(data) {
console.log('Filling array...');
//This is where I think the issue is occurring.
//Is using the name of the section you want to use the correct syntax here?
$.each(data.id, function (index, val) {
planes.push(val.id);
console.log('Pushed ' + index);
});
});
// After getJSON, array should look something like this:
// var planes = [
// 'Alara',
// 'Fiora',
// 'Innistrad',
// 'Kamigawa',
// 'Lorwyn',
// 'Mirrodin',
// 'Ravnica',
// 'Shandalar',
// 'Zendikar'
// ];
的JSON文件安排像這樣:
[
{"id": "Ravnica"},
{"id": "Lorwyn"},
{"id": "Innistrad"},
{"id": "Zendikar"},
{"id": "Kamigawa"},
{"id": "Mirrodin"},
{"id": "Shandalar"},
{"id": "Alara"},
{"id": "Fiora"}
]
任何幫助深表感謝。
順便說一句,你使用mtgjson.com的數據集?如果沒有,那可以爲你的項目節省很多時間...... –