• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    html格式化输出JSON示例(测试接口)

    发布者: 怀142 | 发布时间: 2025-8-13 00:24| 查看数: 49| 评论数: 0|帖子模式

    将 json 数据以美观的缩进格式显示出来,借助最简单的 JSON.stringify 函数就可以了,因为此函数还有不常用的后面2个参数。
    见MDN https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify 的描述。
    示例代码如下:
    1. <html>

    2.     <head>

    3.         <meta charset="utf-8" />



    4.         <title>hello</title>



    5.         <style>

    6.             pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }

    7.             .string { color: green; }

    8.             .number { color: darkorange; }

    9.             .boolean { color: blue; }

    10.             .null { color: magenta; }

    11.             .key { color: red; }

    12.         </style>

    13.         <script type="text/javascript">

    14.         function syntaxHighlight(json) {

    15.             if (typeof json != 'string') {

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

    17.             }

    18.             json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');

    19.             return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {

    20.                 var cls = 'number';

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

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

    23.                         cls = 'key';

    24.                     } else {

    25.                         cls = 'string';

    26.                     }

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

    28.                     cls = 'boolean';

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

    30.                     cls = 'null';

    31.                 }

    32.                 return '<span class="' + cls + '">' + match + '</span>';

    33.             });

    34.         }



    35.     </script>

    36.     </head>

    37.     <body>



    38.     <pre id="result">



    39.     </pre>

    40.     <script type="text/javascript">

    41.         var songResJson={  

    42.               "service": "ALL",  

    43.               "qt": 581,  

    44.               "content": {  

    45.                 "answer": {  

    46.                   "song": "如果缘只到遇见",  

    47.                   "album": "如果缘只到遇见",  

    48.                   "artist": "吴奇隆 严艺丹",  

    49.                   "pic_url": "http://p1.music.126.net/-u3WgIXsFNCW7d8Jy7pCEA==/5921969627395387.jpg"

    50.                 },  

    51.                 "scene": "music"

    52.               }  

    53.             }

    54.             document.getElementById('result').innerHTML = syntaxHighlight(songResJson);



    55.         // $('#result').html(syntaxHighlight(songResJson));

    56.     </script>

    57.      

    58.     </body>

    59. </html>
    复制代码
    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    最新评论

    浏览过的版块

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表