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

 

上一篇:图片上传——layui+ssm


下一篇:Java笔记(2020)