Json Series 3 allows you to print the perfect json format

Source: Internet
Author: User

Json Series 3 allows you to print the perfect json format

Public static String format (String jsonStr) {int level = 0; StringBuffer jsonForMatStr = new StringBuffer (); for (int I = 0; I
 
  
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 (getLevelStr (level); 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
  
   


With the above format code, you can print your json file perfectly

The example is still in the bean to json blog.

{"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":10,"day":2,"hours":11,"minutes":11,"month":1,"seconds":4,"time":1423537864497,"timezoneOffset":-480,"year":115},"emptyStr":"","live":true,"name":"xxx","nullStr":"","sameTest":{"sameName2":"sameNamevalue2","sameName1":"sameNamevalue1"}}





Related Article

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.