安装包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.15.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.15.3</version>
</dependency>
使用
序列化
Student student = new Student();
student.setName("张三");
student.setAge(10);
String jsonString = JsonUtil.toJSONString(student);
System.out.println(jsonString);
反序列化
String s = "{\"name\":\"张三\",\"age\":10}";
Student parse = JsonUtil.parse(s, Student.class);
System.out.println(parse);
Student parse1 = JsonUtil.parse(s, new TypeReference<Student>() {
});
System.out.println(parse1);
JavaType javaType = JsonUtil.getCollectionType(Student.class);
Object parse2 = JsonUtil.parse(s, javaType);
System.out.println(parse2);
点击查看完整代码示例(觉得有用,帮忙在githup项目star一下)