Format the JSON string output in Java

Source: Internet
Author: User

The JSON data that is returned is often received when working on an HTTP-related interface test because it does not look intuitive after formatting the input to the console.

In the morning, I wrote a gadget class that formats the JSON string instead of System.out.print.

Package mytest;/** * Format Input Tool class * @author LIZHGB * @date 2015-10-14 * */public final class Formatutil {/** * print input to console * @p Aram Jsonstr * @author lizhgb * @Date 2015-10-14 pm 1:17:22 */public static void Printjson (String jsonstr) {SYSTEM.OUT.P   Rintln (Formatjson (JSONSTR));} /** * Formatted * @param jsonstr * @return * @author lizhgb * @Date 2015-10-14 PM 1:17:35 */public static String Formatjs On (String jsonstr) {if (null = = Jsonstr | |        ". Equals (JSONSTR)) return" ";        StringBuilder sb = new StringBuilder ();        char last = ' n ';        Char current = ' + ';        int indent = 0;            for (int i = 0; i < jsonstr.length (); i++) {last = current;            Current = Jsonstr.charat (i);                    Switch (current) {case ' {': Case ' [': Sb.append (current);                    Sb.append (' \ n ');                    indent++;                    Addindentblank (SB, indent);                Break CasE '} ': Case ': sb.append (' \ n ');                    indent--;                    Addindentblank (SB, indent);                    Sb.append (current);                Break                    Case ', ': sb.append (current);                        if (last! = ' \ \ ') {sb.append (' \ n ');                    Addindentblank (SB, indent);                } break;            Default:sb.append (current);    }} return sb.tostring ();     }/** * Add space * @param SB * @param indent * @author LIZHGB * @Date 2015-10-14 10:38:04            */private static void Addindentblank (StringBuilder sb, int indent) {for (int i = 0; i < indent; i++) {        Sb.append (' \ t '); }    }}


Test:


@Testpublic void JsonPrintTest2 () {String str = "{\" content\ ": \" This is the msg content.\ ", \" tousers\ ": \" user1|user2\ ", \ "msgtype\": \ "texturl\", \ "appkey\": \ "test\", \ "domain\": \ "test\", "+" \ "system\": {\ "wechat\": {\ "safe\": \ "1\"}},\ " Texturl\ ": {\" urltype\ ": \" 0\ ", \" user1\ ": {\" spstatus\ ": \" user01\ ", \" workid\ ": \" work01\ "},\" user2\ ": {\" Spstatus\ " : \ "user02\", \ "workid\": \ "Work02\"}} "; Formatutil.printjson (str);}

Output:

{"Content": "This is the MSG content.", "tousers": "User1|user2", "Msgtype": "Texturl", "Appkey": "Test", "Domain": "Test", "System": {"WeChat": {"safe": "1"}, "Texturl": {"Urltype": "0", "user1": {"Spstatus": "User01", "WorkID": "Work01"}, " User2 ": {" Spstatus ":" User02 "," WorkID ":" Work02 "}}}




Format the JSON string output in Java

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.