格式化json数据
来源:互联网 发布:opencv java 高斯模糊 编辑:程序博客网 时间:2024/05/17 03:23
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... 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; }); return formatted;};
0 0
- 格式化JSON数据字符串
- 格式化json数据
- Json数据在线格式化
- json数据格式化
- 格式化输出json数据
- json字符串 格式化数据
- sublime格式化json数据
- JSON数据格式化展示
- 格式化json数据
- python 格式化Json数据
- Notepad++格式化JSON数据
- php JSON数据格式化方法
- PHP格式化输出json数据
- php JSON数据格式化方法 .
- JS格式化/压缩JSON数据
- php JSON数据格式化方法
- Jquery格式化显示Json数据
- php JSON数据格式化方法
- 利用onMeasure测量来实现图片拉伸永不变形,解决屏幕适配问题
- PostgreSQL学习手册
- 日语学习之沪江N3基础 20150626 -3
- Android代码混淆
- firefox插件篇
- 格式化json数据
- storage - cinder(1)
- ECLISPE的快捷键大全
- 图片添加水印文字方法 图片批量添加水印教程
- Android常见问题及开发经验总结(一)
- ArcSDE版本学习总结
- 程序猿们,快用Emoji表情写代码吧
- 位结构体详解
- 0.设计模式思想