我用的是这个:
$.ajax({
dataType: "json",
url: "Bibliothek.json",
data: data,
success: function(response) {
console.log(response, "response");
},
});
给我 json 文件的响应,如下所示: https://pastebin.com/2LU5DwRr
我想为每个对象创建一个表,但我失败了,因为我以前从未使用过对象。
$.ajax({
dataType: "json",
url: "Bibliothek.json",
data: data,
success: function(response) {
console.log(response, "response");
let lib = response;
var allarrays = lib.length;
if (allarrays > 0) {
var table = document.createElement("table");
table.style.width = '50%';
table.setAttribute('border', '1');
table.setAttribute('cellspacing', '0');
table.setAttribute('cellpadding', '5');
var col = [];
for (var i = 0; i < allarrays; i++) {
for (var key in lib[i]) {
if (col.indexOf(key) === -1) {
col.push(key);
}
}
};
}
},
});
我知道我的代码无法正常工作,因为 lib 的长度为 1,因为它不会查看每个对象。 我只是不知道如何访问所有对象。
最佳答案
如果您有像 Pastebin 中的嵌套对象,您可以使用内置的对象方法,如 Object.keys()、Object.values() 和 Object.entries() 来循环处理您正在处理的响应对象。
关于javascript - 如何从包含多个对象的 json 文件创建表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59680803/