我有一个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