在html中显示JSON数据的方法


当前第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

2

<pre id="result">

</pre>

调用代码:

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数据》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...