我的浏览器是360的,之前google浏览器也用过格式化显示json的扩展插件,忘了叫什么,自己去扩展中心找找
浏览器扩展中心, 搜索 json 找到 iFormatTool ,点击安装即可
刷新下要显示格式化json的页面,如下:
注意:后端需要返回 json格式化缩进对齐后的数据(带换行符)
java 后端json字符串转 json 格式化字符串 工具类
public final class JSONFormatUtil {
public static void printJson(String jsonStr) {
System.out.println(formatJson(jsonStr));
}
public static String formatJson(String jsonStr) {
if (null == jsonStr || "".equals(jsonStr))
return "";
StringBuilder sb = new StringBuilder();
char last = '\0';
char current = '\0';
int indent = 0;
boolean isInQuotationMarks = false;
for (int i = 0; i < jsonStr.length(); i++) {
last = current;
current = jsonStr.charAt(i);
switch (current) {
case '"':
if (last != '\\'){
isInQuotationMarks = !isInQuotationMarks;
}
sb.append(current);
break;
case '{':
case '[':
sb.append(current);
if (!isInQuotationMarks) {
sb.append('\n');
indent++;
addIndentBlank(sb, indent);
}
break;
case '}':
case ']':
if (!isInQuotationMarks) {
sb.append('\n');
indent--;
addIndentBlank(sb, indent);
}
sb.append(current);
break;
case ',':
sb.append(current);
if (last != '\\' && !isInQuotationMarks) {
sb.append('\n');
addIndentBlank(sb, indent);
}
break;
default:
sb.append(current);
}
}
return sb.toString();
}
private static void addIndentBlank(StringBuilder sb, int indent) {
for (int i = 0; i < indent; i++) {
sb.append('\t');
}
}
}