JSON series three lets you print the perfect JSON format

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.