Jackson使用
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
public static void jackJsonTest() throws IOException {
Map<String, Object> map = new HashMap<>(16);
map.put("id", 1);
map.put("list", List.of(1, 2, 3, 4, 5));
map.put("type", null);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(map));
System.out.println(objectMapper.writeValueAsString(map));
String str = "{\n" +
" \"id\" : 1,\n" +
" \"list\" : [ 1, 2, 3, 4, 5 ]\n" +
"}";
Object o = objectMapper.readValue(str, Object.class);
System.out.println(o.toString());
}