• 技术文章 >web前端 >js教程

    js格式化json的实例代码

    小云云小云云2019-05-24 16:39:01原创2131
    本文主要和大家分享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中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:javascript json 格式化
    上一篇:一个简单的js冒泡排序示例 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• Angular开发问题记录:组件拿不到@Input输入属性• 浅析Node发起HTTP请求的6种方法• 带你了解Nodejs中的非阻塞异步IO• 聊聊基于Node实现单点登录(SSO)的方法• 深入了解Angular(新手入门指南)
    1/1

    PHP中文网