データを取得したいjsonファイルがあります。json ファイルが配列に含まれていない場合、すべてが正常に機能します。問題は、.each 関数で正しいデータを選択する方法がわからないことだと思います。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 ファイルから「chapter2」データを削除すると、すべてが正常に機能します。「chapter1」コンテンツと「chapter2」コンテンツを表示したいと思います。