html 형식의 출력 JSON

巴扎黑
풀어 주다: 2017-09-16 10:18:56
원래의
2138명이 탐색했습니다.

本篇文章主要介绍了html格式化输出JSON示例(测试接口) ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

将 json 数据以美观的缩进格式显示出来,借助最简单的 JSON.stringify 函数就可以了,因为此函数还有不常用的后面2个参数。

见MDN https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify 的描述。

示例代码如下:


<html>

    <head>

        <meta charset="utf-8" />

 

        <title>hello</title>

 

        <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>

        <script type="text/javascript">

        function syntaxHighlight(json) {

            if (typeof json != &#39;string&#39;) {

                json = JSON.stringify(json, undefined, 2);

            }

            json = json.replace(/&/g, &#39;&&#39;).replace(/</g, &#39;<&#39;).replace(/>/g, &#39;>&#39;);

            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 = &#39;number&#39;;

                if (/^"/.test(match)) {

                    if (/:$/.test(match)) {

                        cls = &#39;key&#39;;

                    } else {

                        cls = &#39;string&#39;;

                    }

                } else if (/true|false/.test(match)) {

                    cls = &#39;boolean&#39;;

                } else if (/null/.test(match)) {

                    cls = &#39;null&#39;;

                }

                return &#39;<span class="&#39; + cls + &#39;">&#39; + match + &#39;</span>&#39;;

            });

        }

 

    </script>

    </head>

    <body>

 

    <pre id="result">

 

    
로그인 후 복사

위 내용은 html 형식의 출력 JSON의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!