一引入依赖
<!--Jackson是spring-boot-starter-json的一个依赖(spring-boot-starter-web中包含spring-boot-starter-json)。也就是说,当项目中引入spring-boot-starter-web后会自动引入spring-boot-starter-json -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 简洁代码和方便打印 -->
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version>
</dependency>
二测试的实体
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable,Cloneable {private static final long serialVersionUID = 6447932156561222739L;private Integer sNo;private String name;@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")private Date birthday;private Integer classNo;private String grade;/*** 方便对象复制* @return* @throws CloneNotSupportedException*/@Overrideprotected Student clone() throws CloneNotSupportedException {return (Student) super.clone();}
}
三转json(序列化)
先声明一个类的全局变量
private static ObjectMapper objectMapper = new ObjectMapper();
- 实体(JavaBean)转json
Student s1 = new Student(1,"张三",new Date(),1001,"一年级");
String s1Json = objectMapper.writeValueAsString(s1);
log.info("jackson将Student转的json为{}",s1Json);
测试结果:
jackson将Student转的json为{“name”:“张三”,“birthday”:“2024-06-16 16:42:07”,“classNo”:1001,“grade”:“一年级”,“sno”:1}
2. Map转json
Student s1 = new Student(1,"张三",new Date(),1001,"一年级");
Student s2 = s1.clone();
s2.setSNo(2);
Student s3 = s1.clone();
s3.setSNo(3);
Student s4 = s1.clone();
s4.setSNo(4);
Map<String,Student> data = new LinkedHashMap();
data.put("s1",s1);
data.put("s2",s2);
data.put("s3",s3);
data.put("s4",s4);String mapJson = objectMapper.writeValueAsString(data);
log.info("jackson将map转的json为{}",mapJson);
测试结果:
jackson将map转的json为{“s1”:{“name”:“张三”,“birthday”:“2024-06-16 16:42:07”,“classNo”:1001,“grade”:“一年级”,“sno”:1},“s2”:{“name”:“张三”,“birthday”:“2024-06-16 16:42:07”,“classNo”:1001,“grade”:“一年级”,“sno”:2},“s3”:{“name”:“张三”,“birthday”:“2024-06-16 16:42:07”,“classNo”:1001,“grade”:“一年级”,“sno”:3},“s4”:{“name”:“张三”,“birthday”:“2024-06-16 16:42:07”,“classNo”:1001,“grade”:“一年级”,“sno”:4}}
3. List转json
Student s1 = new Student(1,"张三",new Date(),1001,"一年级");
Student s2 = s1.clone();
s2.setSNo(2);
Student s3 = s1.clone();
s3.setSNo(3);
Student s4 = s1.clone();
s4.setSNo(4);List<Student> studentList = new ArrayList<>(Arrays.asList(s1,s2,s3,s4));
String listJson = objectMapper.writeValueAsString(studentList);
log.info("jackson将List转的json为{}",listJson);
测试结果:
jackson将List转的json为[{“name”:“张三”,“birthday”:“2024-06-16 16:42:07”,“classNo”:1001,“grade”:“一年级”,“sno”:1},{“name”:“张三”,“birthday”:“2024-06-16 16:42:07”,“classNo”:1001,“grade”:“一年级”,“sno”:2},{“name”:“张三”,“birthday”:“2024-06-16 16:42:07”,“classNo”:1001,“grade”:“一年级”,“sno”:3},{“name”:“张三”,“birthday”:“2024-06-16 16:42:07”,“classNo”:1001,“grade”:“一年级”,“sno”:4}]
四转java对象(反序列化)
- 转实体(JavaBean)
Student s1 = new Student(1,"张三",new Date(),1001,"一年级");
String s1Json = objectMapper.writeValueAsString(s1);
log.info("jackson将Student转的json为{}",s1Json);Student json1Student1 = objectMapper.readValue(s1Json,Student.class);
log.info("jackson转实体Student结果:{}",json1Student1);JavaType studentType1 = objectMapper.getTypeFactory().constructType(Student.class);
Student json1Student2 = objectMapper.readValue(s1Json,studentType1);
log.info("jackson转实体Student结果:{}",json1Student2);Student json1Student3 = objectMapper.readValue(s1Json, new TypeReference<Student>() {});
log.info("jackson转实体Student结果:{}",json1Student3);
测试结果:
jackson转实体Student结果:Student(sNo=1, name=张三, birthday=Sun Jun 16 16:42:07 CST 2024, classNo=1001, grade=一年级)
2. 转Map
Student s1 = new Student(1,"张三",new Date(),1001,"一年级");
Student s2 = s1.clone();
s2.setSNo(2);
Student s3 = s1.clone();
s3.setSNo(3);
Student s4 = s1.clone();
s4.setSNo(4);
Map<String,Student> data = new LinkedHashMap();
data.put("s1",s1);
data.put("s2",s2);
data.put("s3",s3);
data.put("s4",s4);String mapJson = objectMapper.writeValueAsString(data);
log.info("jackson将map转的json为{}",mapJson);Map<String,Student> json2map = objectMapper.readValue(mapJson,Map.class);
log.info("jackson转Map结果:{}",json2map);
测试结果:
jackson转Map结果:{s1={name=张三, birthday=2024-06-16 16:42:07, classNo=1001, grade=一年级, sno=1}, s2={name=张三, birthday=2024-06-16 16:42:07, classNo=1001, grade=一年级, sno=2}, s3={name=张三, birthday=2024-06-16 16:42:07, classNo=1001, grade=一年级, sno=3}, s4={name=张三, birthday=2024-06-16 16:42:07, classNo=1001, grade=一年级, sno=4}}
3. 转List
Student s1 = new Student(1,"张三",new Date(),1001,"一年级");
Student s2 = s1.clone();
s2.setSNo(2);
Student s3 = s1.clone();
s3.setSNo(3);
Student s4 = s1.clone();
s4.setSNo(4);
List<Student> studentList = new ArrayList<>(Arrays.asList(s1,s2,s3,s4));String listJson = objectMapper.writeValueAsString(studentList);
log.info("jackson将List转的json为{}",listJson);JavaType listType = objectMapper.getTypeFactory().constructCollectionType(List.class,Student.class);
List<Student> json2List = objectMapper.readValue(listJson,listType);
log.info("jackson转List结果:{}",json2List);
测试结果:
jackson转List结果:[Student(sNo=1, name=张三, birthday=Sun Jun 16 16:42:07 CST 2024, classNo=1001, grade=一年级), Student(sNo=2, name=张三, birthday=Sun Jun 16 16:42:07 CST 2024, classNo=1001, grade=一年级), Student(sNo=3, name=张三, birthday=Sun Jun 16 16:42:07 CST 2024, classNo=1001, grade=一年级), Student(sNo=4, name=张三, birthday=Sun Jun 16 16:42:07 CST 2024, classNo=1001, grade=一年级)]
五常用注解
六其他用法
转自IT利刃出鞘
Jackson用例相关链接