1. 简介
Fastjson 是阿里巴巴开源的高性能 JSON 解析库,专为 Java 设计,具备超快的 JSON 解析速度和丰富的功能。它支持 JSON 与 Java 对象之间的序列化与反序列化,适用于 Web 应用、分布式系统、缓存等场景。
本文将介绍 Fastjson 的基础概念、使用方法、常见实践以及最佳实践,帮助开发者高效使用 Fastjson 进行 JSON 处理。
2. 目录
- Fastjson 介绍
- Fastjson 依赖配置
- JSON 序列化和反序列化
- 使用 JSONPath 查询 JSON 数据
- 配置 Fastjson 特性
- 常见实践
- 安全性注意事项
- 总结
- 参考资料
3. Fastjson 依赖配置
在 pom.xml
中添加 Fastjson 依赖:
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.83</version>
</dependency>
注意:Fastjson 1.x 版本存在安全漏洞,建议使用 2.x 版本:
<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>2.0.37</version>
</dependency>
4. JSON 序列化和反序列化
4.1 Java 对象转 JSON 字符串(序列化)
import com.alibaba.fastjson.JSON;class User {private String name;private int age;public User(String name, int age) {this.name = name;this.age = age;}// 省略 getter 和 setter 方法
}public class FastjsonExample {public static void main(String[] args) {User user = new User("Alice", 25);String jsonString = JSON.toJSONString(user);System.out.println(jsonString);}
}
输出:
{"age":25,"name":"Alice"}
4.2 JSON 字符串转 Java 对象(反序列化)
String json = "{\"age\":25,\"name\":\"Alice\"}";
User user = JSON.parseObject(json, User.class);
System.out.println(user.getName()); // Alice
4.3 解析 JSON 数组
String jsonArray = "[{\"name\":\"Alice\",\"age\":25},{\"name\":\"Bob\",\"age\":30}]";
List<User> users = JSON.parseArray(jsonArray, User.class);
System.out.println(users.get(1).getName()); // Bob
5. 使用 JSONPath 查询 JSON 数据
Fastjson 提供 JSONPath
以便高效查询 JSON 结构化数据。
5.1 查询 JSON 中的某个字段
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;String json = "{\"name\":\"Alice\",\"age\":25,\"address\":{\"city\":\"Shanghai\",\"zip\":\"200000\"}}";
Object city = JSONPath.read(json, "$.address.city");
System.out.println(city); // Shanghai
5.2 查询 JSON 数组中的对象
String jsonArray = "[{\"name\":\"Alice\",\"age\":25},{\"name\":\"Bob\",\"age\":30}]";
List<Object> names = (List<Object>) JSONPath.read(jsonArray, "$[*].name");
System.out.println(names); // [Alice, Bob]
6. 配置 Fastjson 特性
Fastjson 提供了丰富的配置选项,例如:
6.1 格式化 JSON 输出
String prettyJson = JSON.toJSONString(user, true);
System.out.println(prettyJson);
6.2 序列化时忽略 null
值
String jsonString = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
6.3 禁止循环引用
String jsonString = JSON.toJSONString(user, SerializerFeature.DisableCircularReferenceDetect);
7. 常见实践
7.1 处理复杂 JSON 结构
JSONObject jsonObject = JSON.parseObject("{\"data\":{\"user\":{\"name\":\"Alice\"}}}");
String name = jsonObject.getJSONObject("data").getJSONObject("user").getString("name");
System.out.println(name); // Alice
7.2 JSON 转 Map
Map<String, Object> map = JSON.parseObject(json, new TypeReference<Map<String, Object>>() {});
System.out.println(map.get("name"));
7.3 JSON 转 Java BeanList
List<User> userList = JSON.parseArray(jsonArray, User.class);
System.out.println(userList.get(0).getName());
8. 安全性注意事项
Fastjson 存在反序列化漏洞,尤其是 1.x 版本,容易遭受反序列化攻击。建议采取以下措施:
- 升级到 Fastjson 2.x(如
2.0.37
),避免已知漏洞。 - 启用安全模式:
ParserConfig.getGlobalInstance().setSafeMode(true);
- 禁用自动类型解析:
JSON.parseObject(json, Object.class, Feature.SupportAutoType);
- 使用白名单机制:
ParserConfig.getGlobalInstance().addAccept("com.example.");
9. 总结
本文介绍了 Java Fastjson 的核心功能,包括:
- Fastjson 依赖配置
- JSON 序列化和反序列化
- JSONPath 高效查询 JSON 数据
- Fastjson 配置项及最佳实践
- 安全性注意事项
Fastjson 是一个高效、易用的 JSON 处理库,但使用时应注意安全性,特别是在处理不可信数据时,务必禁用自动类型解析或启用安全模式。
10. 参考资料
- Fastjson GitHub
- Fastjson 2.x 官方文档
- JSONPath 语法