濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > html頁面展示json數(shù)據(jù)并格式化的方法

html頁面展示json數(shù)據(jù)并格式化的方法

熱門標(biāo)簽:強(qiáng)訊外呼系統(tǒng) 愛巢地圖標(biāo)注 貴陽ai外呼系統(tǒng) 電銷外呼線路改不外呼線路 重慶人工智能電銷機(jī)器人報(bào)價(jià) 長春極信防封電銷卡公司 智能電銷機(jī)器人廣告語 crm外呼系統(tǒng)好不好 電話機(jī)器人批發(fā)

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號(hào)","age":16, "email": "123456@qq.com","Object":[{"職位":"經(jīng)理"}],"del":[] }</td>
        </tr>
        <tr>
            <td>小四</td>
            <td class="show-rough">{ "name": "小四", "address":"上海路01號(hào)","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)文章,希望大家以后多多支持腳本之家!

標(biāo)簽:保定 清遠(yuǎn) 山南 上海 廣安 陜西 吳忠 內(nèi)蒙古

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《html頁面展示json數(shù)據(jù)并格式化的方法》,本文關(guān)鍵詞  html,頁面,展示,json,數(shù)據(jù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《html頁面展示json數(shù)據(jù)并格式化的方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于html頁面展示json數(shù)據(jù)并格式化的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    松江区| 巴彦县| 乌兰县| 建阳市| 灵璧县| 楚雄市| 沅江市| 湘阴县| 建德市| 墨江| 广西| 邹城市| 闽清县| 民勤县| 农安县| 万州区| 万荣县| 天长市| 峨眉山市| 北安市| 武川县| 社会| 民县| 措美县| 晋宁县| 民和| 霸州市| 抚远县| 东海县| 汕尾市| 平塘县| 鄄城县| 横山县| 宁远县| 石城县| 九龙县| 穆棱市| 伊宁县| 潜江市| 贺州市| 林西县|