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

    html页面展示json数据并格式化的方法

    发布者: 浪子 | 发布时间: 2025-8-13 07:27| 查看数: 78| 评论数: 0|帖子模式

    json数据在html页面展示并格式化
    一、展现效果图

    描述信息:
         
    • key值全部采用红色标出,表示重要参数;   
    • value值采用不同颜色标出,数值类型的采用橘黄色,字符串采用绿色,布尔采用蓝色。。。
    二、源代码展示
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    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.     .showinfo{
    13.         position: absolute;
    14.         background-color: #eef1f8;
    15.         width: 200px;
    16.         padding: 5px;
    17.         border-radius: 4px;
    18.         border: 1px solid #ccc;
    19.         display: none;
    20.     }
    21.     .showinfo pre{
    22.         padding: 5px;
    23.         border: 1px solid #ccc;
    24.         margin:0;
    25.     }
    26.     table,th,td{
    27.         border:1px solid blue;
    28.     }
    29. </style>
    30. <script src="./js/jquery-1.8.3.min.js"></script>
    31. <script type="text/javascript">

    32.     $(document).ready(function(){
    33.         $(".show-rough").mouseover(function(){
    34.             var left = $(this).offset().left + $(this).width() +20;//计算div显示位置
    35.             var top = $(this).offset().top + 20;
    36.             var _jsonDate = $.parseJSON($(this).text());
    37.             var showJson = syntaxHighlight(_jsonDate);
    38.             $("#show-info").css({"left":left,"top":top}).show();
    39.             $("#show-pre").html(showJson);
    40.         });
    41.         $(".show-rough").mouseout(function(){
    42.             $("#show-info").hide().html();
    43.             $("#show-pre").html();
    44.         })
    45.     });
    46.     //处理json数据,采用正则过滤出不同类型参数
    47.         function syntaxHighlight(json) {
    48.     if (typeof json != 'string') {
    49.         json = JSON.stringify(json, undefined, 2);
    50.     }
    51.     json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
    52.     return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {
    53.         var cls = 'number';
    54.         if (/^"/.test(match)) {
    55.             if (/:$/.test(match)) {
    56.                 cls = 'key';
    57.             } else {
    58.                 cls = 'string';
    59.             }
    60.         } else if (/true|false/.test(match)) {
    61.             cls = 'boolean';
    62.         } else if (/null/.test(match)) {
    63.             cls = 'null';
    64.         }
    65.         return '<span class="' + cls + '">' + match + '</span>';
    66.     });
    67. };
    68. </script>
    69. </head>
    70. <body>
    71. <table>
    72.     <thead>
    73.         <tr>
    74.             <th>姓名</th>
    75.             <th>json数据</th>
    76.         </tr>
    77.     </thead>
    78.     <tbody>
    79.         <tr>
    80.             <td>小三</td>
    81.             <td class="show-rough">{ "name": "小三", "address":"北京路23号","age":16, "email": "123456@qq.com","Object":[{"职位":"经理"}],"del":[] }</td>
    82.         </tr>
    83.         <tr>
    84.             <td>小四</td>
    85.             <td class="show-rough">{ "name": "小四", "address":"上海路01号","age":27, "email": "222222@qq.com","Object":[],"del":[]  }</td>
    86.         </tr>
    87.     </tbody>
    88. </table>
    89. <div id="show-info" class="showinfo">
    90.     <pre id="show-pre">

    91. </pre>
    92. </div>
    93. </body>
    94. </html>
    复制代码
    三、源代码上传
    源代码下载地址
    到此这篇关于html页面展示json数据并格式化的方法的文章就介绍到这了,更多相关html展示json并格式化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

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

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

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

    Powered by Discuz! X3.5 © 2001-2023

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