html頁面展示json數(shù)據(jù)并格式化的方法
發(fā)布時(shí)間:2020-06-23 15:59:36 作者:A七秒的記憶
我要評論
這篇文章主要介紹了html頁面展示json數(shù)據(jù)并格式化的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
json數(shù)據(jù)在html頁面展示并格式化
一、展現(xiàn)效果圖

描述信息:
- key值全部采用紅色標(biāo)出,表示重要參數(shù);
- value值采用不同顏色標(biāo)出,數(shù)值類型的采用橘黃色,字符串采用綠色,布爾采用藍(lán)色。。。
二、源代碼展示
<!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;//計(jì)算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數(shù)據(jù),采用正則過濾出不同類型參數(shù)
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數(shù)據(jù)</th>
</tr>
</thead>
<tbody>
<tr>
<td>小三</td>
<td class="show-rough">{ "name": "小三", "address":"北京路23號","age":16, "email": "123456@qq.com","Object":[{"職位":"經(jīng)理"}],"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>
三、源代碼上傳
到此這篇關(guān)于html頁面展示json數(shù)據(jù)并格式化的方法的文章就介紹到這了,更多相關(guān)html展示json并格式化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章

Html5頁面內(nèi)使用JSON動畫的實(shí)現(xiàn)
這篇文章主要介紹了Html5頁面內(nèi)使用JSON動畫的實(shí)現(xiàn)的相關(guān)資料,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-29- 這篇文章主要介紹了HTML5中使用json對象的實(shí)例代碼,需要的朋友可以參考下2018-09-10

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



