【发布时间】:2019-07-21 11:02:23
【问题描述】:
我正在尝试转换以表格形式出现的 SQL 查询的输出。我已将表格转换为 JSON。现在我正在将 JSON 转换为 HTML 表格,以便我可以将其用于报告。
脚本如下,
var value = '{"root":{"row":[{"DatabaseID":"21","fileid":"1","databaseName":"AutomationPortal","FileLogicalName":"AutomationPortal","FileFullPath":"D:\\\\MSSQL13.MSSQLSERVER\\\\MSSQL\\\\DATA\\\\AutomationPortal.mdf","FileSizeMB":"100.00","SpaceUsedMB":"10.25","MaxfileSizeMB":"-0.01","SPaceOnVolumeMB":"95110.38","AutogrowSetting":"8192"},{"DatabaseID":"21","fileid":"3","databaseName":"AutomationPortal","FileLogicalName":"AutomatioPortal_01","FileFullPath":"D:\\\\MSSQL13.MSSQLSERVER\\\\MSSQL\\\\DATA\\\\AutomatioPortal_01.ndf","FileSizeMB":"100.00","SpaceUsedMB":"0.06","MaxfileSizeMB":"130.00","SPaceOnVolumeMB":"95110.38","AutogrowSetting":"8192"}]}}'
var data = JSON.parse(value);
var tablearray = [];
tablearray.push("<table><tr>")
var queryRow = data.root.row.length;
var headerProperty = Object.keys(data.root.row[0]);
for (i=0;i<headerProperty.length;i++){
tablearray.push("<th>"+headerProperty[i]+"</th>");
}
tablearray.push("</tr>");
//console.log(tablearray);
for (i=0;i<queryRow;i++){
tablearray.push("<tr>")
for (j=0;j<headerProperty.length;j++){
// console.log(headerProperty[j]);
// console.log(data.root.row[0].DatabaseID);
// console.log(data.root.row[i].headerProperty[j]);
tablearray.push("<td>"+data.root.row[i].headerProperty[j]+"</td>");
}
tablearray.push("</tr>");
}
tablearray.push("</table>");
tablearray.join('');
当我运行上面的脚本时,它给了我以下错误,我无法解决这个问题。
tablearray.push(""+data.root.row[i].headerProperty[j]+""); ^
TypeError:无法读取未定义的属性“0” 在对象。 (C:\Users\convertjsontohtml.js:21:55) 在 Module._compile (internal/modules/cjs/loader.js:678:30) 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10) 在 Module.load (internal/modules/cjs/loader.js:589:32) 在 tryModuleLoad (internal/modules/cjs/loader.js:528:12) 在 Function.Module._load (internal/modules/cjs/loader.js:520:3) 在 Function.Module.runMain (internal/modules/cjs/loader.js:719:10) 启动时(内部/bootstrap/node.js:228:19) 在 bootstrapNodeJSCore (internal/bootstrap/node.js:575:3)
我期望的输出类似于“”
【问题讨论】:
标签: javascript html json