其他分享
首页 > 其他分享> > jackson之类型转换

jackson之类型转换

作者:互联网

1. pom文件引入jackson

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

 

2. 类中注入并使用

// 创建ObjectMapper对象
@Resource
private ObjectMapper objectMapper;

// 反序列化JSON到对象
Student student = mapper.readValue(jsonString, Student.class);

// 序列化对象到JSON
jsonString = mapper.writeValueAsString(student);

// 转为JsonNode
JsonNode jsonNode = mapper.readTree(jsonString);

// list操作
List<Map<String, String>> mps = new ArrayList<>();
String mapString = mapper.writeValueAsString(mps);
JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, StudentVo.class);
List<StudentVo> StudentVos = mapper.readValue(mapString, javaType);

 

标签:类型转换,mapper,jsonString,jackson,序列化,class,ObjectMapper
来源: https://www.cnblogs.com/wq-code/p/12915676.html