Welcome 微信登录

首页 / 脚本样式 / JavaScript / 正则表达式优化JSON字符串的技巧

json字符串很有用,有时候一些后台接口返回的信息是字符串格式的,可读性很差,这个时候要是有个可以格式化并高亮显示json串的方法那就好多了,下面看看一个正则表达式完成的json字符串的格式化与高亮显示

首先是对输入进行转换,如果是对象则转化为规范的json字符串,不是对象时,先将字符串转化为对象(防止不规范的字符串),然后再次转化为json串。其中json为输入。

if (typeof json !== "string") {json = JSON.stringify(json);} else {json = JSON.parse(json);json = JSON.stringify(json);} 
等规范完数据之后对字符串进行标记,为了后面的切分、重新组合

这里有几个地方要添加标记,包括大括号、小括号的前后和逗号的后面都要添加标记,我这里使用的是换行 (这样在命令行下测试时效果会比较好看)。

// 在大括号前后添加换行reg = /([{}])/g;json = json.replace(reg, "
$1
");// 中括号前后添加换行reg = /([[]])/g;json = json.replace(reg, "
$1
");// 逗号后面添加换行reg = /(,)/g;json = json.replace(reg, "$1
"); 
添加完成标记之后就要做一些优化处理,去掉多余的换行、去掉逗号前面的换行,这样做是为了在切分是免得出现空串浪费一次循环处理,最后在冒号后面添加空格,看起来更漂亮。

// 去除多余的换行reg = /(

)/g;json = json.replace(reg, "
");// 逗号前面的换行去掉reg = /
,/g;json = json.replace(reg, ",");//冒号前面缩进reg = /:/g;json = json.replace(reg, ": "); 
接下来就是对这个初步处理过的串进行进一步处理了,我会在function(index, node) {}函数中添加逻辑,对每一个切分单元进行处理,包括缩进和美化格式。

$.each(json.split("
"), function(index, node) {}); 
首先说下缩进,缩进的方法很简单,遇到{、[符号时缩进增加1,遇到}、]符号时缩进减少1,否则缩进量不变。

//这里遇到{、[时缩进等级加1,遇到}、]时缩进等级减1,没遇到时缩进等级不变if (node.match(/{$/) || node.match(/[$/)) {indent = 1;} else if (node.match(/}/) || node.match(/]/)) {if (pad !== 0) {pad -= 1;}} else {indent = 0;} 
完成缩进后就该美化高亮显示代码了,这里要用到几个css规则,下面可以看到,对切分单元进行高亮显示的时候这里用正则进行判断,如果匹配到大括号标记为对象class、中括号标记为数组class、属性名称、属性值,一次对这些进行css规则添加,添加完成之后拼接起来就可以了。

.ObjectBrace{color:#00AA00;font-weight:bold;}.ArrayBrace{color:#0033FF;font-weight:bold;}.PropertyName{color:#CC0000;font-weight:bold;}.String{color:#007777;}.Number{color:#AA00AA;}.Comma{color:#000000;font-weight:bold;} //添加代码高亮node = node.replace(/([{}])/g,"<span class="ObjectBrace">$1</span>");node = node.replace(/([[]])/g,"<span class="ArrayBrace">$1</span>");node = node.replace(/(".*")(:)(.*)(,)?/g,"<span class="PropertyName">$1</span>$2$3$4");node = node.replace(/"([^"]*)"(,)?$/g,"<span class="String">"$1"</span><span class="Comma">$2</span>");node = node.replace(/(-?d+)(,)?$/g,"<span class="Number">$1</span><span class="Comma">$2</span>"); 
最后我们看看完整的方法代码(这里我使用了jquery类库),以及测试地址:

要对jsonstr进行美化,这样就可以了APP.format(jsonstr),直接输出至<pre></pre>标签中就可以看到效果,

下面是一个测试地址,http://iforever.sinaapp.com/ 可以进去试一下,看看完整的源代码
<script>var APP=function(){var format=function(json){var reg=null,result="";pad=0,PADDING=" ";if (typeof json !== "string") {json = JSON.stringify(json);} else {json = JSON.parse(json);json = JSON.stringify(json);}// 在大括号前后添加换行reg = /([{}])/g;json = json.replace(reg, "
$1
");// 中括号前后添加换行reg = /([[]])/g;json = json.replace(reg, "
$1
");// 逗号后面添加换行reg = /(,)/g;json = json.replace(reg, "$1
");// 去除多余的换行reg = /(

)/g;json = json.replace(reg, "
");// 逗号前面的换行去掉reg = /
,/g;json = json.replace(reg, ",");//冒号前面缩进reg = /:/g;json = json.replace(reg, ": ");//对json按照换行进行切分然后处理每一个小块$.each(json.split("
"), function(index, node) {var i = 0,indent = 0,padding = "";//这里遇到{、[时缩进等级加1,遇到}、]时缩进等级减1,没遇到时缩进等级不变if (node.match(/{$/) || node.match(/[$/)) {indent = 1;} else if (node.match(/}/) || node.match(/]/)) {if (pad !== 0) {pad -= 1;}} else {indent = 0;}//padding保存实际的缩进for (i = 0; i < pad; i++) {padding += PADDING;}//添加代码高亮node = node.replace(/([{}])/g,"<span class="ObjectBrace">$1</span>");node = node.replace(/([[]])/g,"<span class="ArrayBrace">$1</span>");node = node.replace(/(".*")(:)(.*)(,)?/g,"<span class="PropertyName">$1</span>$2$3$4");node = node.replace(/"([^"]*)"(,)?$/g,"<span class="String">"$1"</span><span class="Comma">$2</span>");node = node.replace(/(-?d+)(,)?$/g,"<span class="Number">$1</span><span class="Comma">$2</span>");result += padding + node + "<br>";pad += indent;});return result;};return {"format":format,};}();</script>
PS:正则表达式替换json字符串的某一项的数字值
aa=aa.replaceAll(""ccfsID":"[0-9]*"", ""ccfsID":""+id1+""");
aa为json字符串,如:
{"items":[{"dishprice":30,"ccfsID":"","order.item.id":1,"zuofaid":"","zuofajiajia":0,"isTaoCan":false,"num":1,"price":30,"name":"","ID":"00000001","lsdishID":"","zuofaname":"","tzs":"","addTime":"2013-05-14"}],"deskId":"00000008"}