json字符串转换为一个复杂的类 R<PageResult>
导入依赖:
xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
</dependency>
方法一:使用ObjectMapper
代码:
java
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
String json = "{\"code\":\"OK\",\"data\":{\"records\":[],\"total\":0}}"; // 示例JSON字符串
ObjectMapper mapper = new ObjectMapper();
R<PageResult<EmployeeAccountRespVO>> result = mapper.readValue(json, new TypeReference<R<PageResult<EmployeeAccountRespVO>>>(){});
方法二:使用Gson
代码:
java
import com.google.gson.Gson;
String json = "{\"code\":\"OK\",\"data\":{\"records\":[],\"total\":0}}"; // 示例JSON字符串
Gson gson = new Gson();
R<PageResult<EmployeeAccountRespVO>> result = gson.fromJson(json,
new TypeToken<R<PageResult<EmployeeAccountRespVO>>>(){}.getType());