我正在开发一个包含大量内容的表项目,我想从外部文件(“products.json”)获取JSON内容,以便拥有更好的数据组织。
这是我的Javascript文件......
var domStructure = "";
jsonObj = {
"products": [
{
"ID": "01",
"Name": "John",
"Status": "Published"
},
{
"ID": "02",
"Name": "Paul",
"Status": "Published"
},
{
"ID": "03",
"Name": "Nick",
"Status": "Blacklist"
}],
};
function getTableRow(id, name, status) {
return '\
<tr>\
<td>' + id + '</td>\
<td>' + name + '</td>\
<td>' + status + '</td>\
</tr>';
}
for(var key in jsonObj.products) {
domStructure += getTableRow(jsonObj.products[key].ID, jsonObj.products[key].Name, jsonObj.products[key].Status);
}
document.getElementById('table').getElementsByTagName('tbody')[0].innerHTML = domStructure;