需要有关解析多级JSON的帮助

时间:2019-12-30 23:38:02

标签: javascript html

我的JSON数据

var verses = '[{"ChapterNumber":1,"Verses":[{"VersesNumber":1,"Verses":"Text Come here 1"},{"VersesNumber":2,"Verses":"Text Come here 2"},{"VersesNumber":3,"Verses":"Text Come here 3"},{"VersesNumber":4,"Verses":"Text Come here 4"},{"VersesNumber":5,"Verses":"Text Come here 5"},{"VersesNumber":6,"Verses":"Text Come here 6"},{"VersesNumber":7,"Verses":"Text Come here 7"},{"VersesNumber":8,"Verses":"Text Come here 8"},{"VersesNumber":9,"Verses":"Text Come here 9"},{"VersesNumber":10,"Verses":"Text Come here 10"},{"VersesNumber":11,"Verses":"Text Come here 11"},{"VersesNumber":12,"Verses":"Text Come here 12"},{"VersesNumber":13,"Verses":"Text Come here 13"},{"VersesNumber":14,"Verses":"Text Come here 14"},{"VersesNumber":15,"Verses":"Text Come here 15"},{"VersesNumber":16,"Verses":"Text Come here 16"},{"VersesNumber":17,"Verses":"Text Come here 17"},{"VersesNumber":18,"Verses":"Text Come here 18"},{"VersesNumber":19,"Verses":"Text Come here 19"},{"VersesNumber":20,"Verses":"Text Come here 20"},{"VersesNumber":21,"Verses":"Text Come here 21"},{"VersesNumber":22,"Verses":"Text Come here 22"},{"VersesNumber":23,"Verses":"Text Come here 23"},{"VersesNumber":24,"Verses":"Text Come here 24"},{"VersesNumber":25,"Verses":"Text Come here 25"}]}]'

我尝试JSON.Parse

var Obj = JSON.parse(verses);
for (var i = 0; i < Obj.length; i++) {
    if (Obj[i].ChapterNumber === ChapterNumber) {
        console.log(Obj[i].ChapterNumber);
    }
}

这个工作非常好,以获得第一个Node 但它无法获得第二个节点或那里的节点 我也尝试将其作为测试代码

console.log(Obj[i].ChapterNumber.Verses)

但没有成功,任何人都可以给我提示。

1 个答案:

答案 0 :(得分:0)

尝试一下:

let chapter = '[{"ChapterNumber":1,"Verses":[{"VersesNumber":1,"Verses":"Text Come here 1"},{"VersesNumber":2,"Verses":"Text Come here 2"},{"VersesNumber":3,"Verses":"Text Come here 3"},{"VersesNumber":4,"Verses":"Text Come here 4"},{"VersesNumber":5,"Verses":"Text Come here 5"},{"VersesNumber":6,"Verses":"Text Come here 6"},{"VersesNumber":7,"Verses":"Text Come here 7"},{"VersesNumber":8,"Verses":"Text Come here 8"},{"VersesNumber":9,"Verses":"Text Come here 9"},{"VersesNumber":10,"Verses":"Text Come here 10"},{"VersesNumber":11,"Verses":"Text Come here 11"},{"VersesNumber":12,"Verses":"Text Come here 12"},{"VersesNumber":13,"Verses":"Text Come here 13"},{"VersesNumber":14,"Verses":"Text Come here 14"},{"VersesNumber":15,"Verses":"Text Come here 15"},{"VersesNumber":16,"Verses":"Text Come here 16"},{"VersesNumber":17,"Verses":"Text Come here 17"},{"VersesNumber":18,"Verses":"Text Come here 18"},{"VersesNumber":19,"Verses":"Text Come here 19"},{"VersesNumber":20,"Verses":"Text Come here 20"},{"VersesNumber":21,"Verses":"Text Come here 21"},{"VersesNumber":22,"Verses":"Text Come here 22"},{"VersesNumber":23,"Verses":"Text Come here 23"},{"VersesNumber":24,"Verses":"Text Come here 24"},{"VersesNumber":25,"Verses":"Text Come here 25"}]}]'
chapter = JSON.parse(chapter);
for (var i = 0; i < chapter.length; i++) {
    console.log('Chapter number: '+chapter[i].ChapterNumber);
    for (var n = 0; n < chapter[i].Verses.length; n++) {
        let verse = chapter[i].Verses[n];
        console.log('Verse number: '+verse.VersesNumber);
        console.log('Verse: '+verse.Verses);
    }
}