1. 在pom文件中添加以下依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
2. 序列化与反序列化
Teacher teacher = new Teacher();
teacher.setName("张三");
Map<String, Teacher> map = new HashMap<>();
map.put("teacher", teacher);
String jsonString = JSON.toJSONString(map);
System.out.println(jsonString);
//{"teacher":{"name":"张三"}}
Map<String, Teacher> object = JSON.parseObject(jsonString, new TypeReference<Map<String, Teacher>>() {});
System.out.println(object);
- 复杂类型反序列化需要通过TypeReference的泛型指定类型