使用 js 美化 json
2017-09-26 14:51
639 查看
//json
美化
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
<
4000
span style="font-size:15px;color:rgb(232,226,183);background-color:rgb(0,0,0);">=
(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;
};
美化
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
<
4000
span style="font-size:15px;color:rgb(232,226,183);background-color:rgb(0,0,0);">=
(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;
};
相关文章推荐
- JST+JSON+AJAX——使用客户端js模版代替服务端数据绑定
- json.js与ExtJS一起使用的问题
- Javascript笔记一 js以及json基础使用说明
- Javascript笔记一 js以及json基础使用说明
- php使用js保存的json cookie 字符串,然后解析成数组,需要注意的事项
- 跨域访问 js 使用json script 跨域访问
- JST+JSON+AJAX——使用客户端js模版代替服务端数据绑定
- 使用Beautify.js来美化你的jQuery代码
- 【2012年最新原创AJAX案例,重磅推出】1、使用ajax+js+json+dom+php+mysql实现超强 Web聊天室V2.0
- js中 JSON的使用全解
- 基于backbone.js使用localstorage来缓存请求的json数据。
- js中使用json
- 使用JQuery.js & JQuery.form.js 插件完成对StrutsAction的异步请求,返回JSON数据
- C#、JSON、JS List转换为JSON在前台获取使用
- JST+JSON+AJAX——使用客户端js模版代替服务端数据绑定
- 使用jquery.json把js对象转换成json
- json.js与ExtJS一起使用的问题
- 使用Beautify.js来美化你的jQuery代码
- json,js,java使用总结
- js与php之间使用json格式进行数据交互