最近和一个第三方的系统对接,需要把他们的请求参数保存下来,但是那一长串的json不好读,于是想格式化一下,这种工具有很多,但我想自己弄个来玩下。
刚开始的实现用了很多判断,逻辑很绕。后来想起之前读过一个模板引擎的源码,里面用了状态图,于是我也尝试用了下。
public class Main {
private static String NEW_LINE = "\r\n";
public static void main(String[] args) {
String json = "[{\"name\": \"wen\",\"age\": 12,\"flag\": true,\"job\": [{\"name\":\"java\"},{\"name\": \"c++\"}]},{\"name\": \"yun\",\"age\": 13,\"flag\": false}]";
System.out.println(format(json));
}
private static String format(String json) {
// 去除原来的格式
json = json.replace("\n", "").replace("\r", "").replace("\t", "");
StringBuilder sb = new StringBuilder();
int prevStatus = 0;// 上一状态
int level = 0;// 缩进层级
for (char c : json.toCharArray()) {
int oper = getOperation(prevStatus, c);
switch (oper) {
case 1:
sb.append(NEW_LINE).append(getTab(level));
break;
case 2:
level++;
sb.append(NEW_LINE).append(getTab(level));
break;
case 3:
level--;
sb.append(NEW_LINE).append(getTab(level));
break;
case 4:
sb.append(‘ ‘);
break;
}
sb.append(c);
prevStatus = getStatus(c);
}
return sb.toString();
}
// 返回:0直接输出,1换行,2增加缩进并换行,3减少缩进并换行,4前面加空格
private static int[][] statusArr = new int[][] {
{0, 0, 0, 0, 0, 3}, // 普通字符
{1, 2, 2, 0, 0, 0}, // {[
{2, 0, 4, 0, 0, 0}, // :
{3, 1, 4, 0, 0, 0}, // ,
{4, 0, 0, 0, 0, 3},};// }]
// 根据前一状态和当前字符决定操作
private static int getOperation(int status, char c) {
return statusArr[status][getStatus(c) + 1];
}
// 字符转换成对应状态
private static int getStatus(char c) {
int status = 0;
switch (c) {
case ‘{‘:
case ‘[‘:
status = 1;
break;
case ‘:‘:
status = 2;
break;
case ‘,‘:
status = 3;
break;
case ‘}‘:
case ‘]‘:
status = 4;
break;
}
return status;
}
// 缩进
private static String getTab(int level) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < level; i++) {
sb.append(" ");
}
return sb.toString();
}
}
执行结果
[
{
"name": "wen",
"age": 12,
"flag": true,
"job": [
{
"name":"java"
}, {
"name": "c++"
}
]
}, {
"name": "yun",
"age": 13,
"flag": false
}
]