json数据在html页面展示并格式化
一、展现效果图
描述信息:
- key值全部采用红色标出,表示重要参数;
- value值采用不同颜色标出,数值类型的采用橘黄色,字符串采用绿色,布尔采用蓝色。。。
二、源代码展示
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-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; } .showinfo{ position: absolute; background-color: #eef1f8; width: 200px; padding: 5px; border-radius: 4px; border: 1px solid #ccc; display: none; } .showinfo pre{ padding: 5px; border: 1px solid #ccc; margin:0; } table,th,td{ border:1px solid blue; } </style> <script src="./js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".show-rough").mouseover(function(){ var left = $(this).offset().left + $(this).width() +20;//计算div显示位置 var top = $(this).offset().top + 20; var _jsonDate = $.parseJSON($(this).text()); var showJson = syntaxHighlight(_jsonDate); $("#show-info").css({"left":left,"top":top}).show(); $("#show-pre").html(showJson); }); $(".show-rough").mouseout(function(){ $("#show-info").hide().html(); $("#show-pre").html(); }) }); //处理json数据,采用正则过滤出不同类型参数 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>'; }); }; </script> </head> <body> <table> <thead> <tr> <th>姓名</th> <th>json数据</th> </tr> </thead> <tbody> <tr> <td>小三</td> <td class="show-rough">{ "name": "小三", "address":"北京路23号","age":16, "email": "123456@qq.com","Object":[{"职位":"经理"}],"del":[] }</td> </tr> <tr> <td>小四</td> <td class="show-rough">{ "name": "小四", "address":"上海路01号","age":27, "email": "222222@qq.com","Object":[],"del":[] }</td> </tr> </tbody> </table> <div id="show-info" class="showinfo"> <pre id="show-pre"> </pre> </div> </body> </html>
三、源代码上传
源代码下载地址
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金钱帮资源网 Copyright www.kbjia.com
暂无“html页面展示json数据并格式化的方法”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年06月01日
2024年06月01日
- PUCK《天蓝色的彼岸》[FLAC/分轨][74.02MB]
- Edward Chan《Blueprint of Memories》[320K/MP3][48.3MB]
- 张敬轩.2007-MY1STCOLLECTION【环球】2CD【WAV+CUE】
- 著名歌手绝美音色经典老歌《超级音效王》DTS-WAV
- 流行靓声-群星《情网HQ》DTS[WAV]
- 群星《经典情歌对唱迟来的爱HQ》DTS[WAV]
- 群星2024《传承(太平洋影音四十五周年纪念专辑)4CD》[FLAC]
- 太平洋唱片中国轻音乐1-10全集[竹锦+影视篇+民歌篇+创作篇等]10CD[APE分轨+CUE]
- 群星《2024第一季度百度排行100首》[FLAC/分轨][2.3G]
- 证声音乐图书馆《聆听·心流·爵士 Flow Jazz》[320K/MP3][77.28MB]
- 证声音乐图书馆《聆听·心流·爵士 Flow Jazz》[FLAC/分轨][132.74MB]
- dnf手游为什么不上线了
- dnf手游韩服怎么重新转职
- 雷婷《婷婷醉歌》发烧版K2HD[正版CD低速原抓WAV+CUE]
- 西卿.1997-西北风(布袋戏精选)【金瓜石】【WAV+CUE】