我想从json字符串创建一个表,但是错误来了,我使用了JSON.parse。 我已删除JSON.parse,但无法正常工作。
// javascript function to parse json data//
var parsedJson = JSON.parse(json);
var elements = parsedJson.elements;
function showTable(){
console.log("Runn");
var table = document.getElementById('table');
elements.map(ele => {
var tr = document.createElement("tr");
tr.innerHTML = ele;
table.appendChild(tr);
})
}`
Uncaught SyntaxError: Unexpected token o in JSON at position 1 at
JSON.parse
index.html:599 Runn
index.html:601 Uncaught TypeError: Cannot read property 'map' of undefined
at showTable (index.html:601)
at onload (index.html:614)
答案 0 :(得分:0)
这是您要找的吗?
var json = { "id": 34, "name": "test", "limit": 150000, "tax_type": 34 };
function showTable(){
var table = document.getElementById('table');
var tr = document.createElement("tr");
for (var key in json) {
if (json.hasOwnProperty(key)) {
var td = document.createElement("td");
td.innerHTML = json[key];
tr.appendChild(td);
}
}
table.appendChild(tr);
}
showTable();
table {
border-collapse: collapse;
}
table, td {
border: 1px solid black;
}
<table id="table"></table>