The JSON string is output to a file in a standard format for friendly display

Time:2021-10-14

1. Use Ali’s fastjason

1.1 project pom.xml dependency

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.58</version>
</dependency>
public static void main(String[] args) {

    String jsonString = "{\"_index\":\"book_shop\",\"_type\":\"it_book\",\"_id\":\"1\",\"_score\":1.0," +
            "\"_ Source \ ": {\" name \ ": \" Java programming ideas (4th Edition) \ ", \" author \ ": \" [US] Bruce Eckel \ ", \" category \ ": \" programming language \ "+
            "\" price \ ": 109.0, \" publisher \ ": \" Machinery Industry Press \ ", \" date \ ": \" 2007-06-01 \ ", \" tags \ ": [\" Java \ ", \" programming language \ "]}}";

    JSONObject object = JSONObject.parseObject(jsonString);
    String pretty = JSON.toJSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, 
            SerializerFeature.WriteDateUseDateFormat);

    System.out.println(pretty);
}

 Formatted output results:

Note: fastjason uses the tab key to format the newline

{
    "_index":"book_shop",
    "_type":"it_book",
    "_source":{
        "date":"2007-06-01",
        "Author": "Bruce Eckel",
        "price":109.0,
        "Name": "Java programming ideas (4th Edition)",
        "Publisher": "China Machine Press",
        "Category": "programming language",
        "tags":[
            "Java",
            "Programming language"
        ]
    },
    "_id":"1",
    "_score":1.0
}

1.2 using Google gson

public static void main(String[] args) {
    
    String jsonString = "{\"_index\":\"book_shop\",\"_type\":\"it_book\",\"_id\":\"1\",\"_score\":1.0," +
            "\"_ Source \ ": {\" name \ ": \" Java programming ideas (4th Edition) \ ", \" author \ ": \" [US] Bruce Eckel \ ", \" category \ ": \" programming language \ "+
            "\" price \ ": 109.0, \" publisher \ ": \" Machinery Industry Press \ ", \" date \ ": \" 2007-06-01 \ ", \" tags \ ": [\" Java \ ", \" programming language \ "]}}";
    
    String pretty = toPrettyFormat(jsonString)
    
    System.out.println(pretty);
}

/**
 *Format output JSON string
 *@ return formatted JSON string
 */
private static String toPrettyFormat(String json) {
    JsonParser jsonParser = new JsonParser();
    JsonObject jsonObject = jsonParser.parse(json).getAsJsonObject();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    return gson.toJson(jsonObject);
}

Note: gson uses 2 spaces as the format conversion after line feed

{
  "_index": "book_shop",
  "_type": "it_book",
  "_id": "1",
  "_score": 1.0,
  "_source": {
    "Name": "Java programming ideas (4th Edition)",
    "Author": "Bruce Eckel",
    "Category": "programming language",
    "price": 109.0,
    "Publisher": "China Machine Press",
    "date": "2007-06-01",
    "tags": [
      "Java",
      "Programming language"
    ]
  }
}

Recommended Today

How to use group policy to prohibit win10 automatic update

The premise of using this method is that our windows system must be professional version or enterprise version, because this method uses the “Local Group Policy Editor”, and the windows home version does not directly open the policy group(How to open group policy in win10 home edition? Win10 home group policy open graphic tutorial)So first […]