Java: jackson implements json indentation and beautifies output

rely

<!--json-->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.10.0</version>
</dependency>

Code

public class JsonUtil {
    
    
    /**
     * 美化输出
     */
    public static void prettyPrint(Object obj) {
    
    
        ObjectMapper objectMapper = new ObjectMapper();

        try {
    
    
            String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
            System.out.println(json);
        } catch (JsonProcessingException e) {
    
    
            e.printStackTrace();
        }
    }
}

json example of output control

{
    
    
  "id" : 9,
  "username" : "Panda",
  "password" : "abcd",
  "age" : 20,
  "gender" : "男",
  "email" : "[email protected]"
}

Refer to
[Data Format] Jackson beautifies the output JSON, outputs JSON data elegantly, and formats the output JSON data.

Guess you like

Origin blog.csdn.net/mouday/article/details/132824433
Recommended