html頁面展示json數據并格式化的方法
發(fā)布時間:2020-06-23 15:59:36 作者:A七秒的記憶
我要評論
這篇文章主要介紹了html頁面展示json數據并格式化的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
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>
三、源代碼上傳
到此這篇關于html頁面展示json數據并格式化的方法的文章就介紹到這了,更多相關html展示json并格式化內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持腳本之家!
相關文章
這篇文章主要介紹了Html5頁面內使用JSON動畫的實現的相關資料,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-29- 這篇文章主要介紹了HTML5中使用json對象的實例代碼,需要的朋友可以參考下2018-09-10

基于HTML5的WebGL實現json和echarts圖表展現在同一個界面
這篇文章主要介紹了基于HTML5的WebGL實現json和echarts圖表展現在同一個界面的相關資料,需要的朋友可以參考下2017-10-26- 本篇文章主要介紹了html格式化輸出JSON示例(測試接口) ,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-14
- 這篇文章主要介紹了html格式化json的實例代碼,需要的朋友可以參考下2017-05-22
- 在項目中我們需要將json數據直接顯示在頁面上,但是如果直接顯示字符串很不方便查看,下面小編給大家?guī)砹薶tml中顯示JSON數據的方法,需要的的朋友參考下吧2017-05-10
Html5中l(wèi)ocalStorage存儲JSON數據并讀取JSON數據的實現方法
localStorage是HTML5提供的再客戶端實現本地存儲的一種方法,但是localStorage方法只能存儲字符串數據,有時候我們需要存儲對象到本地比如:JSON;那么,localStorage怎么2017-02-13- 這篇文章主要介紹了舉例詳解HTML5中使用JSON格式提交表單,包括多重數組嵌套等方法的使用演示,需要的朋友可以參考下2015-06-16
- 本文為大家介紹下html table表數據轉Json格式,下面有個不錯的示例,大家可以參考下2014-01-17



