Web頁面json formatter

來源:互聯網
上載者:User

標籤:style   io   os   ar   使用   java   for   sp   on   

外掛程式源碼
定義一個javascript類var JSONFomartter() = {//主方法,參數1為你需要轉換的json對象,注意是對象而不是字串,第二個參數則是你希望的縮排表示字串,例如你可傳進"    "四個空格作為縮排標準format:function(jsonObject, indexStr) {    //如果未傳縮排標準參數,則按預設處理    if (arguments.length < 2) {        var indexStr = "";    }    var indexStrStyle = "    ";    //通過調用方法getType擷取json對象的類型,例如數組    var objectType = getType(jsonObject);    // 根據json物件類型分情況處理    if (objectType == "array") {        if (jsonObject.length == 0) {            return "[]";        }        var sHTML = "[";    } else {        var iCount = 0;        $.each(jsonObject, function() {            iCount++;            return;        });        if (iCount == 0) { // object is empty            return "{}";        }        var sHTML = "{";    }    // 遍曆對象中的key和value並添加縮排    var iCount = 0;    $.each(jsonObject, function(sKey, vValue) {        if (iCount > 0) {            sHTML += ",";        }        if (objectType == "array") {            sHTML += ("\n" + indexStr + indexStrStyle);        } else {            sHTML += ("\n" + indexStr + indexStrStyle + "\"" + sKey + "\"" + ": ");        }        // display relevant data type        switch (getType(vValue)) {            case "array":            case "object":                sHTML += FormatJSON(vValue, (indexStr + indexStrStyle));                break;            case "boolean":            case "number":                sHTML += vValue.toString();                break;            case "null":                sHTML += "null";                break;            case "string":                sHTML += ("\"" + vValue + "\"");                break;            default:                sHTML += ("TYPEOF: " + typeof(vValue));        }        iCount++;    });    if (objectType == "array") {        sHTML += ("\n" + indexStr + "]");    } else {        sHTML += ("\n" + indexStr + "}");    }    return sHTML;},//擷取物件類型getType:function(obj) {  if (typeof(obj) == "object") {    if (obj === null) return "null";    if (obj.constructor == (new Array).constructor) return "array";    if (obj.constructor == (new Date).constructor) return "date";    if (obj.constructor == (new RegExp).constructor) return "regex";    return "object";  }  return typeof(obj);}    }

使用方法

直接調用 JSONFormatter.format(object,"     ");

//例如有這樣的json字串var json = "{‘name‘:‘harry‘,‘sex‘:‘body‘}";//調用JSONFormatter.format(JSON.parse(json));

最佳化

配合highlight.js效果更佳,如下:

{        "result": [            {                "name": "吐槽",                "id": "336efba7-259d-4057-ae1e-679e81419cd7",                "module": "life"            }        ]    }


Web頁面json formatter

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.