JSON 如何正确解析嵌套的 JSON

JSON How to correctly parse a nested JSON?

本文关键字:JSON 嵌套 何正确      更新时间:2023-09-26

我正在尝试解析我的htmldiv中的嵌套JSON对象。我正在尝试获取每个对象的标题。我希望它尽可能干燥(不要重复自己),所以每当我向 json 添加新对象时,它都会自动解析div 中的对象。

我不知道这是否是正确的语法

Object.keys(booklist.Bible)[1].Title

当我尝试使用此代码时,它返回未定义。

<script>
var booklist = {
"Bible":{
"ARV":{"Title":"American Revised Version", "Author":"Unknown", "Icon":"ARV.png"},
"KJV":{"Title":"King James Version", "Author":"King James", "Icon":"KJV.png"},
},
"Bible Commentary":{
"DR":{"Title":"The Prophecies of Daniel and the Revelation by Uriah Smith", "Author":"Uriah Smith", "Icon":"DR.png"},
"EGWBC":{"Title":"Ellen G. White Bible Commentary", "Author":"Ellen G. White", "Icon":"EGWBC.png"}
},
var contents = Object.keys(booklist.Bible)[0];
var catList = document.getElementById('contents');
var category = document.createElement('div');
category.innerHTML='<div class="row">'+
                    '<div class="inline ui-block-a">'+
                        '<img class="thumbs" src="images/default.png">'+
                    '</div>'+
                    '<div class="inline ui-block-b">'+
                        '<h4>'+Object.keys(booklist.Bible)[1].Title+'</h4>'+
                        '<span class="subs"></span>'+
                    '</div>'+
                    '<div class="inline ui-block-c">'+
                        '<h4></h4>'+
                        '<span class="subs">Author</span>'+
                    '</div>'+
                '</div><hr>';
          catList.appendChild(category);
</script>

如果你想访问书名ARV,你可以像访问这样的

booklist.Bible.ARV.Title  

booklist["Bible"]["ARV"]["Title"]

在javascript中就是这么简单。