ExtJS中格式化json显示

简介: , { xtype: 'button', text: '格式化', iconCls: 'icon-edit', colspan: 2, width: 100, margin: "-23 0 0 70", listeners: { 'click': { fn: this.
,  {
    xtype: 'button',
    text: '格式化',
    iconCls: 'icon-edit',
    colspan: 2,
    width: 100,
    margin: "-23 0 0 70",
    listeners: {
        'click': {
            fn: this.formatJson,
            scope: this
        }
    }
}

formatJson : function(options) {
    var dataform = this.down('dataform');
    var json=dataform.down("[name='actConfigValue']").getValue();
    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...
    if (typeof json !== 'string') {
        // make sure we start with the JSON as a string
        json = JSON.stringify(json);
    } else {
        // is already a string, so parse and re-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;
    });
    dataform.down("[name='actConfigValue']").setValue(formatted);
    // return formatted;
},
相关文章
|
JSON 数据格式
JSON - JSON.toJSONString 格式化成 JSON 字符串时保留 null 属性
JSON - JSON.toJSONString 格式化成 JSON 字符串时保留 null 属性
653 0
|
1月前
|
JSON 数据格式 Python
python写入的json文件要格式化
要将JSON格式化后写入文件,你可以在`json.dump()`函数中使用`indent`参数来设置缩进级别。以下是一个示例: ```python import json data = {"name": "John", "age": 30, "city": "New York"} with open('data.json', 'w') as file: json.dump(data, file, indent=4) ``` 在这个示例中,我们使用`json.dump()`函数将Python对象转换为JSON格式,并将其写入到文件中。通过传递`indent=4`参数,我们设置了缩
|
5月前
|
JSON JavaScript 数据格式
jQuery将json性别数据int类型进行格式化渲染
jQuery将json性别数据int类型进行格式化渲染
26 0
|
5月前
|
JSON 前端开发 Java
Java实现树的格式化打印以及返回前端json树
Java实现树的格式化打印以及返回前端json树
31 0
|
5月前
|
JSON Linux 开发工具
linux 利用python模块实现格式化json
linux 利用python模块实现格式化json
46 0
|
8月前
|
JSON JavaScript 前端开发
开发了一个json格式化工具,使用js格式化json的代码分享
今天给大家介绍一下如何通过js来格式化json。
71 0
|
9月前
|
Web App开发 JSON 前端开发
360浏览器安装JSON-Handle插件实现页面JSON数据格式化的操作方法
360浏览器安装JSON-Handle插件实现页面JSON数据格式化的操作方法
189 0
|
JSON Shell 数据格式
shell 中格式化显示 json 字符串
可以利用 `python3` 提供的标准库 `json` 来实现, 或 jq
500 0
|
存储 JSON 前端开发
谁偷了我的1天,JSON格式化时区问题
谁偷了我的1天,JSON格式化时区问题
811 0
谁偷了我的1天,JSON格式化时区问题
|
XML JSON 前端开发
SpringBoot2.x系列教程15--SpringBoot中整合HttpMessageConverters实现JSON格式化
前言 在之前的章节中,壹哥 带着各位学习了如何在Spring Boot中进行SSM整合。那么接下来,我们会继续深入研究SpringBoot对SpringMVC框架的支持,学习SpringBoot如何进行更深度的定制化Web开发。 前面我讲过,SpringBoot严格的来说,应该是一种负责把其他已有框架整合在一起的工具,SpringBoot主要是把各种框架都整合集中在一起,简化我们的Web开发。所以很多的功能,其实都不是SpringBoot完成的,而是由SpringBoot中整合的其他框架来完成的。比如Web开发,更多的是由SpringMVC来完成,只是SpringBoot很好的把Spring
1000 0