import com.fasterxml.jackson.core.type.TypeReference;
2import com.fasterxml.jackson.databind.ObjectMapper;
3
4public class User {
5 // User 类定义省略
6}
7
8public class Main {
9 public static void main(String[] args) throws Exception {
10 ObjectMapper mapper = new ObjectMapper();
11 String jsonString = readFromFile(); // 假设这个方法读取 txt 文件中的 JSON 内容
12
13 // 创建一个 TypeReference 对象来保持 Map<User, User> 的泛型信息
14 TypeReference<Map<User, User>> typeRef
15 = new TypeReference<Map<User, User>>() {};
16
17 Map<User, User> userMap = mapper.readValue(jsonString, typeRef);
18
19 // 现在 userMap 是 Map<User, User> 类型
20 }
21}
ObjectMapper 反解析json
最新推荐文章于 2024-07-23 11:07:53 发布