我有一個json文件,我想從中提取數據。當json文件不在數組中時,一切正常。我想問題是我不知道如何在我的.each函數中選擇正確的數據。下面是例子JSON文件:用jQuery顯示json數組。每個
{
"chapter1": [
{
"title":"This is the title",
"chapter":"1",
"verse":"1",
"text":"text goes here"
},
{
"verse":"4",
"text":"text goes here"
},
{
"verse":"6",
"text":"text goes here"
}
],
"chapter2": [
{
"title":"This is the title",
"chapter":"2",
"verse":"1",
"text":"text goes here"
},
{
"verse":"4",
"text":"text goes here"
},
{
"verse":"6",
"text":"text goes here"
}
]
}
這裏是我的循環中顯示的數據:
$.getJSON(passages, function(data) {
$.each(data, function() {
$('#chapter1 h2').append(this.title);
$('#chapter1 h3').append(this.chapter);
$('#verses').append('<li><p>' + this.verse + this.text + '</p></li>');
});
});
當我從JSON文件中刪除了「第2章」數據,比一切都運行得很好。我想顯示「chapter1」的內容和「chapter2」的內容。
什麼是你用來創建JSON,因爲它看起來像有一些矛盾有 –
這只是一個.json文件我將一些數據插入。 – user715564