JSON字符串控制台格式化输出 java

1.正常情况下返回的json数据格式如下:

{"header":{"transSn":"e33128bb7622462ebfb2cbfcc46baa14","dateTime":"20181002110000","serviceCode":"********","appId":"999999999999","bizId":"000000","version":"1.0","resType":"A","resCode":"SSS000","resMsg":"交易成功(业务附加消息:*****)"},"resBody":{"operator":"lgh","rowNumStart":"1","pageRowNum":"100","pageFlag":"0","totalRowNum":"1","orderFlag":"0","orderField":"","result":[{"flag":"1","riskCode":"00567000","riskName":"567*********","prem":"520.00","amntOrMult":"***","amnt":"200000","polNo":"12177"}]}}

 



默认都是一行输出,或者在HTML进过一些插件格式化之后才会显示比较美观的格式。那么在Java后台如何进行json格式化输出呢下面直接贴代码。

2.可以封装一个工具类专门转换json格式化:

     /**
         * @param resString
         * @return String
         * @throws
         * @Description 响应数据格式化
         * @author lgh
         * @date 2018/10/29-13:45
         */
        private String responseFormat(String resString){
     
            StringBuffer jsonForMatStr = new StringBuffer();
            int level = 0;
            for(int index=0;index<resString.length();index++)//将字符串中的字符逐个按行输出
            {
                //获取s中的每个字符
                char c = resString.charAt(index);
     
                //level大于0并且jsonForMatStr中的最后一个字符为\n,jsonForMatStr加入\t
                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(getLevelStr(level));
                        jsonForMatStr.append(c);
                        break;
                    default:
                        jsonForMatStr.append(c);
                        break;
                }
            }
            return jsonForMatStr.toString();
        }
        /**
         * @param level
         * @return
         * @throws
         * @author lgh
         * @date 2018/10/29-14:29
         */
        private String getLevelStr(int level) {
            StringBuffer levelStr = new StringBuffer();
            for (int levelI = 0; levelI < level; levelI++) {
                levelStr.append("\t");
            }
            return levelStr.toString();
        }

 


3.同样的数据输出之后就达到预期效果:

 

   {
        "header":{
            "transSn":"e33128bb7622462ebfb2cbfcc46baa14",
            "dateTime":"20181002110000",
            "serviceCode":"********",
            "appId":"999999999999",
            "bizId":"000000",
            "version":"1.0",
            "resType":"A",
            "resCode":"SSS000",
            "resMsg":"交易成功(业务附加消息:***)"
        },
        "resBody":{
            "operator":"lgh",
            "rowNumStart":"1",
            "pageRowNum":"100",
            "pageFlag":"0",
            "totalRowNum":"1",
            "orderFlag":"0",
            "orderField":"",
            "result":[
                {
                    "flag":"1",
                    "riskCode":"00567000",
                    "riskName":"567 ****",
                    "prem":"520.00",
                    "amntOrMult":"***",
                    "amnt":"200000",
                    "polNo":"12177"
                }
            ]
        }
    }

 



如有披露或问题欢迎留言或者入群探讨(仅仅是个json格式化的工具分享)
---------------------
作者:一页知秋否
来源:CSDN
原文:https://blog.csdn.net/qq_39470733/article/details/83506752
版权声明:本文为博主原创文章,转载请附上博文链接!

原文 JSON字符串控制台格式化输出

posted @ 2019-05-14 10:19  xiaoshen666  阅读(4164)  评论(0编辑  收藏  举报