public static string format (string jsonstr) {Int. level = 0; StringBuffer jsonformatstr = new StringBuffer (); for (int i=0;i<jsonstr.length (); i++) {char c = jsonstr.charat (i); if (level>0&& ' \ n ' ==jsonformatstr.charat (Jsonformatstr.length ()-1)) {Jsonformatstr.append (GETLEVELSTR ( level)); } switch (c) {case ' {': Case ' [': Jsonformatstr.append (c+ "\ n"); level++; Break Case ', ': jsonformatstr.append (c+ "\ n"); Break Case '} ': Case ': jsonformatstr.append ("\ n"); level--; Jsonformatstr.append (level) (GETLEVELSTR); Jsonformatstr.append (c); Break Default:jsonForMatStr.append (c); Break }} return jsonformatstr.tostring (); } private static String getlevelstr (int level) {StringBuffer LevElstr = new StringBuffer (); for (int leveli = 0;leveli<level; leveli++) {levelstr.append ("\ t"); } return levelstr.tostring (); }
With the format code above, you can make your JSON print flawlessly.
Example is still the bean to JSON blog post example
{"Addresses": [{"Address": "Address1", "sametest": [{"Addresses": [], "age": 0, "attrs": null, "baseaddress": null, " BirthDay ": null," EMPTYSTR ":" "," Live ": false," name ":" Samename "," Nullstr ":" "," sametest ": null}]},{" Address ":" Address2 "," sametest ": []}]," Age ": 1," Attrs ": {" Key2 ":" value2 "," Key1 ":" Value1 "}," BaseAddress ": {" Address ":" Base Address "," sametest ": []}," BirthDay ": {" date ": Ten," Day ": 2," hours ": one," minutes ": One," month ": 1," Seconds ": 4," Time " : 1423537864497, "Timezoneoffset": -480, "Year": [+], "emptyStr": "," Live ": True," name ":" XXX "," nullstr ":" "," sametest ": {" sameName2 ":" SameNamevalue2 "," sameName1 ":" SameNamevalue1 "}}
Three of the JSON series lets you print the perfect JSON format