提问人:Ganesh Putta 提问时间:10/3/2023 更新时间:10/3/2023 访问量:96
如何把JSON DATA转换为html表格
how to convert JSON DATA into html table
问:
我尝试使用一些示例,但不知何故它不起作用。请指导我。
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
td, th {
padding: 10px;
}
</style>
</head>
<body>
<h2>Convert JSON data into a html table using Javascript</h2>
<p>Click the following button to convert JSON results into HTML table</p><br>
<button id="btn" onclick="convert( )"> Click Here </button> <br>
<h3> Resulting Table: </h3>
<div id="container"></div>
<script>
function convert() {
// Sample JSON data
let jsonData = {
"data":[
{
"customer Name": "ganesh",
"ami":"T6767667",
"state":"closed",
"ssoId":"dhhhdhv",
"convId":"I90098890098",
"account number":"2233233"
},
{
"customer Name": "putta",
"ami":"T6767668",
"state":"closed",
"ssoId":"dhhhdhY",
"convId":"I90098890099",
"account number":"2233234"
}
],
"metadata":{
"": "",
"outcome":{
"status":200,
"code":0,
"type":"OK",
"message":""
}
}
};
let container = document.getElementById("container");
let table = document.createElement("table");
let cols = Object.keys(jsonData[0]);
let thead = document.createElement("thead");
let tr = document.createElement("tr");
cols.forEach((item) => {
let th = document.createElement("th");
th.innerText = item;
tr.appendChild(th);
});
thead.appendChild(tr);
table.append(tr)
jsonData.forEach((item) => {
let tr = document.createElement("tr");
let vals = Object.values(item);
vals.forEach((elem) => {
let td = document.createElement("td");
td.innerText = elem;
tr.appendChild(td);
});
table.appendChild(tr);
});
container.appendChild(table)
}
</script>
</body>
</html>
答:
0赞
Vaibhav Patil
10/3/2023
#1
您提供的代码似乎几乎正确,但有一个小问题。您正在尝试访问 j,但 jsonData 不是一个数组,它是一个具有“data”属性的对象,其中包含您要遍历的数组。您应该访问 jsonData.data 以访问sonData[0]
array.pls
我曾经访问过数据对象的数组,并且还为表标题行添加了数据对象。此外,在追加表之前,我已清除容器,以确保在多次单击该按钮时表会更新。jsonData.data
<thead>
代码片段
function convert() {
// Sample JSON data
let jsonData = {
"data": [{
"customer Name": "ganesh",
"ami": "T6767667",
"state": "closed",
"ssoId": "dhhhdhv",
"convId": "I90098890098",
"account number": "2233233"
},
{
"customer Name": "putta",
"ami": "T6767668",
"state": "closed",
"ssoId": "dhhhdhY",
"convId": "I90098890099",
"account number": "2233234"
}
],
"metadata": {
"": "",
"outcome": {
"status": 200,
"code": 0,
"type": "OK",
"message": ""
}
}
};
let container = document.getElementById("container");
let table = document.createElement("table");
let cols = Object.keys(jsonData.data[0]);
let thead = document.createElement("thead");
let tr = document.createElement("tr");
cols.forEach((item) => {
let th = document.createElement("th");
th.innerText = item;
tr.appendChild(th);
});
thead.appendChild(tr);
table.appendChild(thead);
jsonData.data.forEach((item) => {
let tr = document.createElement("tr");
let vals = Object.values(item);
vals.forEach((elem) => {
let td = document.createElement("td");
td.innerText = elem;
tr.appendChild(td);
});
table.appendChild(tr);
});
container.innerHTML = '';
container.appendChild(table);
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
td,
th {
padding: 10px;
}
<h2>Convert JSON data into an HTML table using JavaScript</h2>
<p>Click the following button to convert JSON results into an HTML table</p><br>
<button id="btn" onclick="convert()"> Click Here </button> <br>
<h3> Resulting Table: </h3>
<div id="container"></div>
评论