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);

 

posted @ 2020-05-19 10:56  品味Code  阅读(1551)  评论(0编辑  收藏  举报