简体   繁体   English

使用JavaScript将html与表标签转换为JSON

[英]convert JSON with html table tag using JavaScript

I am trying to convert output from SQL Query which comes as a table. 我正在尝试将SQL Query的输出转换为表格。 I have converted the table as JSON. 我已将表转换为JSON。 Now I am in a process of converting the JSON to HTML Table so that I can use it for reporting. 现在,我正在将JSON转换为HTML表,以便可以将其用于报告。

Script is given below, 脚本如下

 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(''); 

When I run the above script it gives me the following error, I am unable to fix the issue. 当我运行上面的脚本时,它给我以下错误,我无法解决此问题。

tablearray.push(""+data.root.row[i].headerProperty[j]+""); tablearray.push(“” + data.root.row [i] .headerProperty [j] +“”); ^ ^

TypeError: Cannot read property '0' of undefined at Object. TypeError:无法读取对象上未定义的属性“ 0”。 (C:\\Users\\convertjsontohtml.js:21:55) at Module._compile (internal/modules/cjs/loader.js:678:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10) at Module.load (internal/modules/cjs/loader.js:589:32) at tryModuleLoad (internal/modules/cjs/loader.js:528:12) at Function.Module._load (internal/modules/cjs/loader.js:520:3) at Function.Module.runMain (internal/modules/cjs/loader.js:719:10) at startup (internal/bootstrap/node.js:228:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:575:3) (C:\\ Users \\ convertjsontohtml.js:21:55)在Object.Module._extensions..js(internal / modules / cjs / loader)的Module._compile(internal / modules / cjs / loader.js:678:30) .js:689:10)位于Function.Module._load的tryModuleLoad(内部/模块/cjs/loader.js:528:12)的Module.load(内部/模块/cjs/loader.js:589:32)(内部/模块/cjs/loader.js:520:3)在启动时(内部/bootstrap/node.js:228:19)在Function.Module.runMain(内部/模块/cjs/loader.js:719:10)在bootstrapNodeJSCore(内部/引导/node.js:575:3)

The Output I am expecting is like "" 我期望的输出像“”

You can build the table by looping thought each value like this: 您可以通过循环考虑每个值来构建表,如下所示:

 const input = '{"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"}]}}'; // Parse given JSON const parsed = JSON.parse(input); // Get keys (=cells) of each items const keys = Object.keys(parsed.root.row[0]); // Build the table header const header = `<thead><tr>` + keys .map(key => `<th>${key}</th>`) .join('') + `</thead></tr>`; // Build the table body const body = `<tbody>` + parsed.root.row .map(row => `<tr>${Object.values(row) .map(cell => `<td>${cell}</td>`) .join('')}</tr>` ).join(''); // Build the final table const table = ` <table> ${header} ${body} </table> `; // Append the result into #root element document.getElementById('root').innerHTML = table; 
 <div id="root"></div> 

data.root.row [0]内部没有headerProperty

Instead of working with strings you might want to create elements using document.createElement 您可能不希望使用字符串来使用document.createElement创建元素

 const 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"}]}}' const data = JSON.parse(value); console.log(data); const $table = document.createElement('table'); const $head = document.createElement('thead'); const $body = document.createElement('tbody'); for (let i = 0; i < data.root.row.length; i++) { const $tr = document.createElement('tr'); Object.keys(data.root.row[0]).forEach((colName) => { if (i === 0) { const $th = document.createElement('th'); $th.innerText = colName; $head.appendChild($th); } const $td = document.createElement('td'); $td.innerText = data.root.row[0][colName]; $tr.appendChild($td); }); $body.appendChild($tr); } $table.appendChild($head); $table.appendChild($body); document.getElementById('table').appendChild($table); 
 <div id="table"></div> 

The problem is that your row s don't have a property called "headerProperty". 问题是您的row没有名为“ headerProperty”的属性。 I think you are wanting to use the value inside headerProperty[j] as a dynamic property name? 我认为您想使用headerProperty[j]作为动态属性名称?

For that you have to use "bracket notation" to write the property accessor - this allows you to use any string value as the property name at runtime: 为此,您必须使用“括号表示法”编写属性访问器-这允许您在运行时使用任何字符串值作为属性名称:

data.root.row[i][propertyHeader[j]]

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors for a bit more info. 有关更多信息,请参见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors

Demo - I hope this now outputs what you expected: 演示-我希望现在可以输出您期望的结果:

 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>"); document.write(tablearray.join('')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM