How to format and output JSON data to the console in Java

Time:2019-11-14

In the process of interface testing, most data interaction uses JSON format, but the effect of outputting JSON in console is far less beautiful than that of browser plug-in. After looking for some data, I decided to write a method to format and output JSON information to the console, which has been completed after some attempts. Share as follows:

/**
     * output JSON
     *
     *@ param jsonobject JSON format response entity
     */
    public static JSONObject output(JSONObject jsonObject) {
        if (MapUtils.isEmpty(jsonObject)) {
            Output ("the JSON object is empty! "";
            return jsonObject;
        }
        String start = SourceCode.getManyString(SPACE_1, 4);
        String jsonstr = jsonobject. Tostring(); // first convert the JSON object to a string object
        jsonStr = jsonStr.replaceAll("\\/", OR);
        Int level = 0; // user tag level
        StringBuffer jsonresultstr = new StringBuffer (">
        For (int i = 0; I < jsonstr. Length(); I + +) {// loop through each character
            Char piece = jsonstr.charat (I); // get the current character
            //If the previous character is a line break, the first line is excluded by adding a marker according to the level value at the beginning of the line
            if (i != 0 && '\n' == jsonResultStr.charAt(jsonResultStr.length() - 1)) {
                for (int k = 0; k < level; k++) {
                    jsonResultStr.append(start);
                }
            }
            switch (piece) {
                case ',':
                    //If it is "," break line
                    char last = jsonStr.charAt(i - 1);
                    if ("\"0123456789le]}".contains(last + EMPTY)) {
                        jsonResultStr.append(piece + LINE);
                    } else {
                        jsonResultStr.append(piece);
                    }
                    break;
                case '{':
                case '[':
                    //If the character is {or [, line break, level plus 1
                    jsonResultStr.append(piece + LINE);
                    level++;
                    break;
                case '}':
                case ']':
                    //If} or], line break, level minus 1
                    jsonResultStr.append(LINE);
                    level--;
                    for (int k = 0; k < level; k++) {
                        jsonResultStr.append(start);
                    }
                    jsonResultStr.append(piece);
                    break;
                default:
                    jsonResultStr.append(piece);
                    break;
            }
        }
        output(LINE + "↘ ↘ ↘ ↘ ↘ ↘ ↘ ↘ json ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙" + LINE + jsonResultStr.toString().replaceAll(LINE, LINE + ">  ") + LINE + "↘ ↘ ↘ ↘ ↘ ↘ ↘ ↘ json ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙ ↙");
        return jsonObject;
    }

Updated output display:

How to format and output JSON data to the console in Java

Note: there are “,” in JSON data, and there may be some display exceptions when value is not string (number and null or science and technology method), and when value itself is a string of JSON data, value will also be treated as JSON. Overall, it’s enough, and the effect is quite satisfactory.

Come on ~ funtester

Selection of previous articles

  1. Java one line code printing heart
  2. Chinese version of Linux performance monitoring software netdata
  3. Interface test code coverage (jacoco) scheme sharing
  4. Performance testing framework
  5. How to test the performance of Linux command line interface happily
  6. Diagram HTTP brain map
  7. Programming thinking for all
  8. JVM command brain diagram of test

Public map