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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM