JSON格式化输出 Java代码
作者:
斯特的简书 | 来源:发表于
2018-07-24 17:48 被阅读12次 public class JsonFormater {
public static String format(String json) {
StringBuilder indent = new StringBuilder();//缩进
StringBuilder sb = new StringBuilder();
for (char c : json.toCharArray()) {
switch (c) {
case '{':
indent.append(" ");
sb.append("{\n").append(indent);
break;
case '}':
indent.deleteCharAt(indent.length() - 1);
sb.append("\n").append(indent).append("}");
break;
case '[':
indent.append(" ");
sb.append("[\n").append(indent);
break;
case ']':
indent.deleteCharAt(indent.length() - 1);
sb.append("\n").append(indent).append("]");
break;
case ',':
sb.append(",\n").append(indent);
break;
default:
sb.append(c);
}
}
return sb.toString();
}
}
本文标题:JSON格式化输出 Java代码
本文链接:https://www.haomeiwen.com/subject/poeymftx.html
网友评论