1. Jackson(Spring Boot 默认支持,无需额外依赖)
1.1 添加依赖(如果使用 Spring Boot,默认已有,无需添加)
如果你不是 Spring Boot 项目,需要手动添加 Jackson 依赖:
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.15.0</version>
</dependency>
1.2 Jackson - 序列化(Java 对象 → JSON)
使用 ObjectMapper
进行对象转换:
java">import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;public class JacksonExample {public static void main(String[] args) {ObjectMapper objectMapper = new ObjectMapper();// 创建一个 Java 对象User user = new User(1, "张三", 25, "developer");try {// Java 对象序列化为 JSONString jsonStr = objectMapper.writeValueAsString(user);System.out.println("序列化后的 JSON: " + jsonStr);} catch (JsonProcessingException e) {e.printStackTrace();System.err.println("JSON 序列化失败:" + e.getMessage());}}
}
1.3 Jackson - 反序列化(JSON → Java 对象)
java">import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;public class JacksonExample {public static void main(String[] args) {ObjectMapper objectMapper = new ObjectMapper();// JSON 字符串String jsonStr = "{\"id\":1,\"name\":\"张三\",\"age\":25,\"job\":\"developer\"}";try {// JSON 反序列化为 Java 对象User user = objectMapper.readValue(jsonStr, User.class);System.out.println("反序列化后的对象: " + user);} catch (JsonProcessingException e) {e.printStackTrace();System.err.println("JSON 反序列化失败:" + e.getMessage());}}
}
1.4 Jackson - 处理 JSON 转换异常
Jackson 会抛出 JsonProcessingException
,可以进行异常处理:
java">try {String invalidJson = "{\"id\":1,\"name\":\"张三\",\"age\":\"invalid\",\"job\":\"developer\"}";User user = objectMapper.readValue(invalidJson, User.class);
} catch (JsonProcessingException e) {System.err.println("JSON 解析错误:" + e.getMessage());
}
1.5 Jackson - 复杂类型(Map、List、泛型)
Jackson 还可以解析 Map
、List
和泛型:
java">import com.fasterxml.jackson.core.type.TypeReference;String jsonList = "[{\"id\":1,\"name\":\"张三\"},{\"id\":2,\"name\":\"李四\"}]";// 反序列化为 List
List<User> userList = objectMapper.readValue(jsonList, new TypeReference<List<User>>() {});
System.out.println(userList);
2. FastJSON 2.x(高性能 JSON 解析)
2.1 添加依赖
FastJSON 1.x 有安全漏洞,推荐使用 FastJSON 2.x:
<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>2.0.39</version>
</dependency>
2.2 FastJSON - 序列化(Java 对象 → JSON)
java">import com.alibaba.fastjson2.JSON;public class FastJsonExample {public static void main(String[] args) {// 创建 Java 对象User user = new User(1, "张三", 25, "developer");// FastJSON 序列化String jsonStr = JSON.toJSONString(user);System.out.println("FastJSON 序列化后的 JSON: " + jsonStr);}
}
2.3 FastJSON - 反序列化(JSON → Java 对象)
java">import com.alibaba.fastjson2.JSON;public class FastJsonExample {public static void main(String[] args) {// JSON 字符串String jsonStr = "{\"id\":1,\"name\":\"张三\",\"age\":25,\"job\":\"developer\"}";// FastJSON 反序列化User user = JSON.parseObject(jsonStr, User.class);System.out.println("FastJSON 反序列化后的对象: " + user);}
}
2.4 FastJSON - 处理 JSON 转换异常
FastJSON 2.x 提供了 JSON.parseObject(json, clazz)
,但如果数据格式错误,会抛出 JSONException
:
java">try {String invalidJson = "{\"id\":1,\"name\":\"张三\",\"age\":\"invalid\",\"job\":\"developer\"}";User user = JSON.parseObject(invalidJson, User.class);
} catch (Exception e) {System.err.println("FastJSON 解析错误:" + e.getMessage());
}
2.5 FastJSON - 复杂类型(Map、List、泛型)
FastJSON 也可以解析 Map
和 List
:
java">import com.alibaba.fastjson2.TypeReference;// JSON 数组
String jsonList = "[{\"id\":1,\"name\":\"张三\"},{\"id\":2,\"name\":\"李四\"}]";// 反序列化为 List
List<User> userList = JSON.parseObject(jsonList, new TypeReference<List<User>>() {});
System.out.println(userList);
3. Jackson vs FastJSON 对比
特性 | Jackson(Spring 默认) | FastJSON 2.x(高性能) |
---|---|---|
序列化性能 | 快 | 更快(FastJSON 2.x 进行了优化) |
反序列化性能 | 快 | 更快 |
Spring Boot 兼容性 | 默认支持 | 需要额外引入 |
安全性 | 更安全 | FastJSON 1.x 有漏洞,2.x 已修复 |
特性 | 功能丰富,支持 JSON Schema、XML、YAML | API 简单,适合大数据量处理 |