久久久久在线观看_又色又爽又黄的免费视频播放_一区中文字幕_日韩电影在线播放

今日焦點 焦點資訊 營銷之道 企業報道 淘寶運營 網站建設 軟件開發 400電話
  當前位置: 首頁 » 資訊 » 軟件開發 » 正文

js格式化json的實例代碼

放大字體  縮小字體 發布日期:2018-03-02  來源:企業800網  作者:新格網  瀏覽次數:669  【去百度看看】
核心提示:本文主要和大家分享js格式化json的實例代碼,希望能幫助到大家。
本文主要和大家分享js格式化json的實例代碼,希望能幫助到大家。

var jsonStr;// json字符串
// 格式化json字符串
var jsonFormat = formatJson(jsonStr);
// 工具方法
var formatJson = function(json, options) {
                    var reg = null,
                        formatted = '',
                        pad = 0,
                        PADDING = '    '; // one can also use '\t' or a different number of spaces
                    // optional settings
                    options = options || {};
                    // remove newline where '{' or '[' follows ':'
                    options.newlineAfterColonIfBeforeBraceOrBracket = (options.newlineAfterColonIfBeforeBraceOrBracket === true) ? true : false;
                    // use a space after a colon
                    options.spaceAftercolon = (options.spaceAftercolon === false) ? false : true;

                    // begin formatting...

                    // make sure we start with the JSON as a string
                    if (typeof json !== 'string') {
                        json = JSON.stringify(json);
                    }
                    // parse and stringify in order to remove extra whitespace
                    json = JSON.parse(json);
                    json = JSON.stringify(json);

                    // add newline before and after curly braces
                    reg = /([\{\}])/g;
                    json = json.replace(reg, '\r\n$1\r\n');

                    // add newline before and after square brackets
                    reg = /([\[\]])/g;
                    json = json.replace(reg, '\r\n$1\r\n');

                    // add newline after comma
                    reg = /(\,)/g;
                    json = json.replace(reg, '$1\r\n');

                    // remove multiple newlines
                    reg = /(\r\n\r\n)/g;
                    json = json.replace(reg, '\r\n');

                    // remove newlines before commas
                    reg = /\r\n\,/g;
                    json = json.replace(reg, ',');

                    // optional formatting...
                    if (!options.newlineAftercolonIfBeforeBraceOrBracket) {
                        reg = /\:\r\n\{/g;
                        json = json.replace(reg, ':{');
                        reg = /\:\r\n\[/g;
                        json = json.replace(reg, ':[');
                    }
                    if (options.spaceAftercolon) {
                        reg = /\:/g;
                        json = json.replace(reg, ': ');
                    }

                    $.each(json.split('\r\n'), function(index, node) {
                        var i = 0,
                            indent = 0,
                            padding = '';

                        if (node.match(/\{$/) || node.match(/\[$/)) {
                            indent = 1;
                        } else if (node.match(/\}/) || node.match(/\]/)) {
                            if (pad !== 0) {
                                pad -= 1;
                            }
                        } else {
                            indent = 0;
                        }

                        for (i = 0; i < pad; i++) {
                            padding += PADDING;
                        }

                        formatted += padding + node + '\r\n';
                        pad += indent;
                    });

                    return formatted;
                };

效果圖

相關推薦:

html怎樣格式化json數據

格式化Json傳遞日期的方法

html格式化json的實例代碼

以上就是js格式化json的實例代碼的詳細內容,更多請關注php中文網其它相關文章!

 
 
[ 資訊搜索 ]  [ 加入收藏 ]  [ 告訴好友 ]  [ 打印本文 ]  [ 違規舉報 ]  [ 關閉窗口 ]

 
0條 [查看全部]  相關評論

 
網站首頁 | 關于我們 | 聯系方式 | 使用協議 | 版權隱私 | 網站地圖 | 排名推廣 | 廣告服務 | 積分換禮 | 網站留言 | RSS訂閱 | 皖ICP備2021004516號-14
企業800網 · 提供技術支持