Jackson 工具类
示例代码中包含 Date LocalDate LocalDateTime 类型处理方式
JavaBean 与 json 相互转换 bean2json json2bean
List 与 json 相互转换 list2json json2list
Map 与 json 相互转换map2json json2map
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lihaohze</groupId><artifactId>chap06</artifactId><version>1.0.0</version><packaging>jar</packaging><name>chap06</name><url>https://mvnrepository.com/</url><properties><!-- 公共配置 --><maven.compiler.source>21</maven.compiler.source><maven.compiler.target>21</maven.compiler.target><maven.compiler.compilerVersion>21</maven.compiler.compilerVersion><maven.compiler.encoding>utf-8</maven.compiler.encoding><project.build.sourceEncoding>utf-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><maven.test.failure.ignore>true</maven.test.failure.ignore><maven.test.skip>true</maven.test.skip><commons-io.version>2.14.0</commons-io.version><commons-lang3.version>3.13.0</commons-lang3.version><hutool.version>5.8.22</hutool.version><jackson.version>2.15.3</jackson.version><junit.version>5.10.0</junit.version><lombok.version>1.18.30</lombok.version></properties><dependencies><!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>${junit.version}</version><!-- 作用域 --><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>${junit.version}</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version><scope>provided</scope></dependency><!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool.version}</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>${commons-lang3.version}</version></dependency><!-- https://mvnrepository.com/artifact/commons-io/commons-io --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>${commons-io.version}</version></dependency><!--jackson--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.datatype</groupId><artifactId>jackson-datatype-jsr310</artifactId><version>${jackson.version}</version></dependency></dependencies>
</project>
Jackson 工具类
package com.lihaozhe.util.json.jackson;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapType;import java.util.List;
import java.util.Map;/*** @author 李昊哲* @version 1.0* @create 2023/10/19*/
public class JacksonUtils {/*** 实例化 ObjectMapper*/private final static ObjectMapper objectMapper = new ObjectMapper();/*** 将对象转换成json字符串。* <p>Title: pojoToJson</p>* <p>Description: </p>** @param bean JavaBean对象* @return json格式字符串*/public static String bean2json(Object bean) {try {return objectMapper.writeValueAsString(bean);} catch (JsonProcessingException e) {e.printStackTrace();return null;}}/*** 将json结果集转化为对象** @param jsonString json格式字符串* @param beanType 对象类型* @return JavaBean对象*/public static <T> T json2bean(String jsonString, Class<T> beanType) {try {return objectMapper.readValue(jsonString, beanType);} catch (Exception e) {e.printStackTrace();return null;}}/*** 将json数据转换成pojo对象list* <p>Title: jsonToList</p>* <p>Description: </p>** @param jsonString json格式字符串* @param beanType 对象类型* @return List对象*/public static <T> List<T> json2list(String jsonString, Class<T> beanType) {JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, beanType);try {return objectMapper.readValue(jsonString, javaType);} catch (Exception e) {e.printStackTrace();return null;}}/*** 将json数据转换成pojo对象list* <p>Title: jsonToList</p>* <p>Description: </p>** @param jsonString json格式字符串* @param keyType key对象类型* @param valueType value对象类型* @return List对象*/public static <K, V> Map<K, V> json2map(String jsonString, Class<K> keyType, Class<V> valueType) {MapType mapType = objectMapper.getTypeFactory().constructMapType(Map.class, keyType, valueType);try {return objectMapper.readValue(jsonString, mapType);} catch (Exception e) {e.printStackTrace();return null;}}
}
javabean与json格式字符串相互转换
准备javabean
package com.lihaozhe.json.jackson;import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.deser.std.DateDeserializers;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.lihaozhe.util.date.DateUtils;
import lombok.*;import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;/*** @author 李昊哲* @version 1.0* @create 2023/10/19*/
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Emp {/*** 账号*/private String account;/*** 密码*/private String password;/*** 姓名*/private String name;/*** 性别 1 代表男性 0 代表女性*/private int gender;/*** 入职时间*/@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH-mm-ss", locale = "zh", timezone = "GMT+8")@JsonSerialize(using = DateSerializer.class)@JsonDeserialize(using = DateDeserializers.DateDeserializer.class)private Date hiredate;/*** 离职日期*/@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH-mm-ss", locale = "zh", timezone = "GMT+8")@JsonSerialize(using = DateSerializer.class)@JsonDeserialize(using = DateDeserializers.DateDeserializer.class)private Date turnoverDate;/*** 日期*/@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", locale = "zh", timezone = "GMT+8")@JsonSerialize(using = LocalDateSerializer.class)@JsonDeserialize(using = LocalDateDeserializer.class)private LocalDate createDate;/*** 账号停用时间*/@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH-mm-ss", locale = "zh", timezone = "GMT+8")@JsonSerialize(using = LocalDateTimeSerializer.class)@JsonDeserialize(using = LocalDateTimeDeserializer.class)private LocalDateTime createDateTime;public Emp(String account, String password, String name, int gender) {this.account = account;this.password = password;this.name = name;this.gender = gender;this.hiredate = new Date();this.createDate = LocalDate.now();this.createDateTime = LocalDateTime.now();}
}
javabean转json格式字符串
Emp emp = new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1);
System.out.println(emp);
String json = JacksonUtils.bean2json(emp);
System.out.println(json);
结果如下:
Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:58:06 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:58:06.343)
{"account":"root","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 19-58-06","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 19-58-06"}
json格式字符串转javabean
Emp emp = new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1);
System.out.println(emp);
String json = JacksonUtils.bean2json(emp);
System.out.println(json);
结果如下:
Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:58:06 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:58:06.343)
{"account":"root","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 19-58-06","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 19-58-06"}
Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:58:06 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:58:06.343)
list与json格式字符串相互转换
list转json格式字符串
List<Emp> empList = new ArrayList<>();
empList.add(new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1));
empList.add(new Emp("admin", "e10adc3949ba59abbe56e057f20f883e", "李哲", 0));
empList.forEach(System.out::println);
String json = JacksonUtils.bean2json(empList);
System.out.println(json);
结果如下:
Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:59:09 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:59:09.321)
Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 19:59:09 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:59:09.323)
[{"account":"root","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 19-59-09","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 19-59-09"},{"account":"admin","password":"e10adc3949ba59abbe56e057f20f883e","name":"李哲","gender":0,"hiredate":"2023-10-19 19-59-09","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 19-59-09"}]
json格式字符串转list
List<Emp> empList = new ArrayList<>();
empList.add(new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1));
empList.add(new Emp("admin", "e10adc3949ba59abbe56e057f20f883e", "李哲", 0));
empList.forEach(System.out::println);
String json = JacksonUtils.bean2json(empList);
System.out.println(json);
// JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Emp.class);
// List<Emp> emps = objectMapper.readValue(json, javaType);
List<Emp> emps = objectMapper.readValue(json, new TypeReference<List<Emp>>() {});
emps.forEach(System.out::println);
结果如下:
Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:59:09 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:59:09.321)
Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 19:59:09 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:59:09.323)
[{"account":"root","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 19-59-09","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 19-59-09"},{"account":"admin","password":"e10adc3949ba59abbe56e057f20f883e","name":"李哲","gender":0,"hiredate":"2023-10-19 19-59-09","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 19-59-09"}]
Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 19:59:09 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:59:09.321)
Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 19:59:09 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T19:59:09.323)
map与json格式字符串相互转换
map转json格式字符串
Map<String, Emp> map = new HashMap<>();
map.put("root", new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1));
map.put("admin", new Emp("admin", "e10adc3949ba59abbe56e057f20f883e", "李哲", 0));
map.forEach((key, value) -> System.out.println("key >>> " + key + "\tvalue >>> " + value));
String json = JacksonUtils.bean2json(map);
System.out.println(json);
结果如下:
key >>> root value >>> Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 20:01:52 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T20:01:52.568)
key >>> admin value >>> Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 20:01:52 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T20:01:52.570)
{"root":{"account":"root","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 20-01-52","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 20-01-52"},"admin":{"account":"admin","password":"e10adc3949ba59abbe56e057f20f883e","name":"李哲","gender":0,"hiredate":"2023-10-19 20-01-52","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 20-01-52"}}
json格式字符串转map
Map<String, Emp> map = new HashMap<>();
map.put("root", new Emp("root", "e10adc3949ba59abbe56e057f20f883e", "李昊哲", 1));
map.put("admin", new Emp("admin", "e10adc3949ba59abbe56e057f20f883e", "李哲", 0));
map.forEach((key, value) -> System.out.println("key >>> " + key + "\tvalue >>> " + value));
String json = JacksonUtils.bean2json(map);
System.out.println(json);
// MapType mapType = objectMapper.getTypeFactory().constructMapType(Map.class, String.class, Emp.class);
// Map<String, Emp> empMap = objectMapper.readValue(json, mapType);
Map<String, Emp> empMap = objectMapper.readValue(json, new TypeReference<Map<String, Emp>>() {
});
empMap.forEach((key, value) -> System.out.println("key >>> " + key + "\tvalue >>> " + value));
结果如下:
key >>> root value >>> Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 20:01:52 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T20:01:52.568)
key >>> admin value >>> Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 20:01:52 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T20:01:52.570)
{"root":{"account":"root","password":"e10adc3949ba59abbe56e057f20f883e","name":"李昊哲","gender":1,"hiredate":"2023-10-19 20-01-52","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 20-01-52"},"admin":{"account":"admin","password":"e10adc3949ba59abbe56e057f20f883e","name":"李哲","gender":0,"hiredate":"2023-10-19 20-01-52","turnoverDate":null,"createDate":"2023-10-19","createDateTime":"2023-10-19 20-01-52"}}
key >>> root value >>> Emp(account=root, password=e10adc3949ba59abbe56e057f20f883e, name=李昊哲, gender=1, hiredate=Thu Oct 19 20:01:52 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T20:01:52.568)
key >>> admin value >>> Emp(account=admin, password=e10adc3949ba59abbe56e057f20f883e, name=李哲, gender=0, hiredate=Thu Oct 19 20:01:52 CST 2023, turnoverDate=null, createDate=2023-10-19, createDateTime=2023-10-19T20:01:52.570)