66 placeholder =" 请输入json"
77 @change =" inputJson"
88 v-model =" orJson"
9- :autosize =" true"
109 ></el-input >
1110 </div >
1211 <div class =" show-json" >
1312 <el-input
1413 type =" textarea"
14+ v-if =" orJson !== ''"
1515 placeholder =" 显示的json"
1616 v-model =" showJson"
1717 :autosize =" true"
@@ -33,7 +33,9 @@ export default {
3333 },
3434 methods: {
3535 inputJson () {
36- this .doSort (1 );
36+ if (this .orJson !== ' ' ) {
37+ this .doSort (1 );
38+ }
3739 },
3840 doSort (type ) {
3941 let obj;
@@ -97,56 +99,56 @@ export default {
9799 // json对象转换为字符串变量
98100 let jsonString = JSON .stringify (jsonObj);
99101 // 存储需要特殊处理的字符串段
100- const _index = [];
102+ const tempIndex = [];
101103 // 存储需要特殊处理的“再数组中的开始位置变量索引
102- let _indexStart = null ;
104+ let tempIndexStart = null ;
103105 // 存储需要特殊处理的“再数组中的结束位置变量索引
104- let _indexEnd = null ;
106+ let tempIndexEnd = null ;
105107 // 将jsonString字符串内容通过\r\n符分割成数组
106108 let jsonArray = [];
107109 // 正则匹配到{,}符号则在两边添加回车换行
108- jsonString = jsonString .replace (/ ([\{\ } ] )/ g , ' \r\n $1\r\n ' );
110+ jsonString = jsonString .replace (/ ([{ }] )/ g , ' \r\n $1\r\n ' );
109111 // 正则匹配到[,]符号则在两边添加回车换行
110- jsonString = jsonString .replace (/ ([\ [\] ] )/ g , ' \r\n $1\r\n ' );
112+ jsonString = jsonString .replace (/ ([[\] ] )/ g , ' \r\n $1\r\n ' );
111113 // 正则匹配到,符号则在两边添加回车换行
112- jsonString = jsonString .replace (/ (\ , )/ g , ' $1\r\n ' );
114+ jsonString = jsonString .replace (/ (,)/ g , ' $1\r\n ' );
113115 // 正则匹配到要超过一行的换行需要改为一行
114116 jsonString = jsonString .replace (/ (\r\n\r\n )/ g , ' \r\n ' );
115117 // 正则匹配到单独处于一行的,符号时需要去掉换行,将,置于同行
116- jsonString = jsonString .replace (/ \r\n \ ,/ g , ' ,' );
118+ jsonString = jsonString .replace (/ \r\n ,/ g , ' ,' );
117119 // 特殊处理双引号中的内容
118120 jsonArray = jsonString .split (' \r\n ' );
119121 jsonArray .forEach ((node , index ) => {
120122 // 获取当前字符串段中"的数量
121- const num = node .match (/ \ "/ g ) ? node .match (/ \ "/ g ).length : 0 ;
123+ const num = node .match (/ "/ g ) ? node .match (/ "/ g ).length : 0 ;
122124 // 判断num是否为奇数来确定是否需要特殊处理
123- if (num % 2 && ! _indexStart ) {
124- _indexStart = index;
125+ if (num % 2 && ! tempIndexStart ) {
126+ tempIndexStart = index;
125127 }
126- if (num % 2 && _indexStart && _indexStart ! = index) {
127- _indexEnd = index;
128+ if (num % 2 && tempIndexStart && tempIndexStart != = index) {
129+ tempIndexEnd = index;
128130 }
129131 // 将需要特殊处理的字符串段的其实位置和结束位置信息存入,并对应重置开始时和结束变量
130- if (_indexStart && _indexEnd ) {
131- _index .push ({
132- start: _indexStart ,
133- end: _indexEnd ,
132+ if (tempIndexStart && tempIndexEnd ) {
133+ tempIndex .push ({
134+ start: tempIndexStart ,
135+ end: tempIndexEnd ,
134136 });
135- _indexStart = null ;
136- _indexEnd = null ;
137+ tempIndexStart = null ;
138+ tempIndexEnd = null ;
137139 }
138140 });
139141 // 开始处理双引号中的内容,将多余的"去除
140- _index .reverse ().forEach ((item ) => {
142+ tempIndex .reverse ().forEach ((item ) => {
141143 const newArray = jsonArray .slice (item .start , item .end + 1 );
142144 jsonArray .splice (item .start , item .end + 1 - item .start , newArray .join (' ' ));
143145 });
144146 // 奖处理后的数组通过\r\n连接符重组为字符串
145147 jsonString = jsonArray .join (' \r\n ' );
146148 // 将匹配到:后为回车换行加大括号替换为冒号加大括号
147- jsonString = jsonString .replace (/ \ :\r\n \{ / g , ' :{' );
149+ jsonString = jsonString .replace (/ :\r\n \{ / g , ' :{' );
148150 // 将匹配到:后为回车换行加中括号替换为冒号加中括号
149- jsonString = jsonString .replace (/ \ :\r\n \[ / g , ' :[' );
151+ jsonString = jsonString .replace (/ :\r\n \[ / g , ' :[' );
150152 // 将上述转换后的字符串再次以\r\n分割成数组
151153 jsonArray = jsonString .split (' \r\n ' );
152154 // 将转换完成的字符串根据PADDING值来组合成最终的形态
0 commit comments