public static void main(String[] args) { String region = "{\"id\":\"1\",\"name\":\"a1\",\"obj\":{\"id\":11,\"name\":\"a11\",\"array\":[{\"id\":111,\"name\":\"a111\"},{\"id\":112,\"name\":\"a112\"}]}}"; String fotmated = GgsBase.formatToJson(region); // fotmatStr = fotmatStr.replaceAll("\n", ""); // fotmatStr = fotmatStr.replaceAll("\t", " "); System.out.println(fotmated); } /** * get json data backspace use '\t' new line use '\n' */ public static String formatToJson(String region) { int level = 0; StringBuffer preBuffer = new StringBuffer(); for (int i = 0; i < region.length(); i++) { char c = region.charAt(i); if (level > 0 && '\n' == preBuffer.charAt(preBuffer.length() - 1)) { preBuffer.append(getLevelStr(level)); } switch (c) { case '{': case '[': preBuffer.append(c + "\n"); level++; break; case ',': preBuffer.append(c + "\n"); break; case '}': case ']': preBuffer.append("\n"); level--; preBuffer.append(getLevelStr(level)); preBuffer.append(c); break; default: preBuffer.append(c); break; } } return String.valueOf(preBuffer); } private static String getLevelStr(int level) { StringBuffer lb = new StringBuffer(); for (int levelTmp = 0; levelTmp < level; levelTmp++) { lb.append("\t"); } return String.valueOf(lb); }
{ "id":"1", "name":"a1", "obj":{ "id":11, "name":"a11", "array":[ { "id":111, "name":"a111" }, { "id":112, "name":"a112" } ] }}