当前第2页 返回上一页
js代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | function syntaxHighlight(json) {
if (typeof json != 'string' ) {
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, '&' ).replace(/</g, '<' ).replace(/>/g, '>' );
return json.replace(/( "(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\" ])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number' ;
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key' ;
} else {
cls = 'string' ;
}
} else if (/true|false/.test(match)) {
cls = 'boolean' ;
} else if (/null/.test(match)) {
cls = 'null' ;
}
return '<span class="' + cls + '">' + match + '</span>' ;
});
}
|
样式代码:
1 2 3 4 5 6 7 8 | <style>
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
</style>
|
html代码:
调用代码:
1 | $( '#result' ).html(syntaxHighlight(res));
|
效果:

相关推荐:
HTML页面点击下载文件的两种实现方法
以上就是在html中显示JSON数据的方法的详细内容,更多文章请关注木庄网络博客!
返回前面的内容
相关阅读 >>
html button标签的样式怎么设置?html button标签的样式介绍
html怎么画出圆形
你必须知道的10个chrome开发工具和技巧
html server-sent 事件
html文档的基本结构由哪三对标签负责组织
html元素(标签)大全及使用介绍
html中stroke是什么意思?
npm机制深入理解
html中如何设置图片位置
如何给html设置背景
更多相关阅读请进入《html显示json数据》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 在html中显示JSON数据的方法