[英]JSON format in HTML CELL table
Folks,各位,
I have the following requirement:我有以下要求:
I will have a list of log lines in a HTML table, some of these lines will contain JSON strings, I want to format the JSON in table when the HTML file is loaded from disk.我将在 HTML 表中有一个日志行列表,其中一些行将包含 JSON 字符串,我想在从磁盘加载 HTML 文件时格式化表中的 JSON。 I have it nearly there, see JSFiddle.
我已经快到了,请参阅 JSFiddle。 I just cant seem to get it formatted inside the table cell:
我似乎无法在表格单元格内对其进行格式化:
http://jsfiddle.net/yh7bsd51/ http://jsfiddle.net/yh7bsd51/
var table = document.getElementById("report");
for (var i = 0, row; row = table.rows[i]; i++)
{
col = row.cells[0];
//if(innerHTML == JSON)
col.innerHTML = (syntaxHighlight(JSON.stringify(col.innerHTML, undefined, 4)));
}
var s = {"Id":"124578","oId":"11","Type":"2"};
var ss = JSON.stringify(s, undefined, 4);
The problem with the formatting inside the cell is that you were calling JSON.stringify
on a string, not an object.单元格内部格式的问题在于您对字符串而不是对象调用
JSON.stringify
。 Check out this fiddle: http://jsfiddle.net/zu7zs5b1/ .看看这个小提琴: http : //jsfiddle.net/zu7zs5b1/ 。 Note this part:
注意这部分:
for (var i = 0, row; row = table.rows[i]; i++) {
col = row.cells[0];
//if(innerHTML == JSON)
try {
jsn = JSON.stringify(JSON.parse(col.innerHTML), undefined, 4); // <-- proper JSON str
col.innerHTML = (syntaxHighlight(jsn));
} catch (e) {
console.log("Fail");
}
}
A bit hacky, but it gets the job done.有点hacky,但它完成了工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.