[前端] html页面展示json数据并格式化的方法

2061 0
黑夜隐士 2022-10-19 16:04:25 | 显示全部楼层 |阅读模式
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并格式化内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章,希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

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

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行