下面我给你做一个详细对比,介绍四种常见 JSON 库在 Java 中的用法:Gson、Jackson、Fastjson、org.json,以及它们各自的特点和典型操作示例。
1. Gson(Google 出品)
特点:
简单易用,轻量级
支持对象与 JSON 相互转换
不依赖大量注解,适合快速开发
Maven 依赖:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
示例代码:
import com.google.gson.Gson;
import java.util.List;
class User {
String name;
int age;
}
public class GsonExample {
public static void main(String[] args) {
Gson gson = new Gson();
// 对象 -> JSON
User user = new User();
user.name = "Alice";
user.age = 25;
String jsonStr = gson.toJson(user);
System.out.println(jsonStr); // {"name":"Alice","age":25}
// JSON -> 对象
User u2 = gson.fromJson(jsonStr, User.class);
System.out.println(u2.name + " " + u2.age);
}
}
2. Jackson(FasterXML 出品)
特点:
功能强大,企业级常用
支持对象与 JSON 相互转换
注解丰富(
@JsonProperty
、@JsonIgnore
等)性能较高,支持流式处理
Maven 依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.16.0</version>
</dependency>
示例代码:
import com.fasterxml.jackson.databind.ObjectMapper;
class User {
public String name;
public int age;
}
public class JacksonExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
User user = new User();
user.name = "Bob";
user.age = 30;
// 对象 -> JSON
String jsonStr = mapper.writeValueAsString(user);
System.out.println(jsonStr); // {"name":"Bob","age":30}
// JSON -> 对象
User u2 = mapper.readValue(jsonStr, User.class);
System.out.println(u2.name + " " + u2.age);
}
}
3. Fastjson(阿里巴巴出品)
特点:
简单易用,适合快速开发
性能优秀(尤其是反序列化)
支持 JSONPath、序列化配置
安全性注意:早期 Fastjson 存在反序列化漏洞
Maven 依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.89</version>
</dependency>
示例代码:
import com.alibaba.fastjson.JSON;
class User {
public String name;
public int age;
}
public class FastjsonExample {
public static void main(String[] args) {
User user = new User();
user.name = "Charlie";
user.age = 28;
// 对象 -> JSON
String jsonStr = JSON.toJSONString(user);
System.out.println(jsonStr); // {"age":28,"name":"Charlie"}
// JSON -> 对象
User u2 = JSON.parseObject(jsonStr, User.class);
System.out.println(u2.name + " " + u2.age);
}
}
4. org.json(最原始的 JSON 库)
特点:
非常轻量,只提供最基础的 JSONObject、JSONArray 操作
不支持直接对象映射,需要手动构建
適合小型快速工具,但不适合复杂对象
Maven 依赖:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
</dependency>
示例代码:
import org.json.JSONObject;
public class OrgJsonExample {
public static void main(String[] args) {
JSONObject obj = new JSONObject();
obj.put("name", "David");
obj.put("age", 35);
// JSONObject -> JSON字符串
String jsonStr = obj.toString();
System.out.println(jsonStr); // {"name":"David","age":35}
// JSON字符串 -> JSONObject
JSONObject obj2 = new JSONObject(jsonStr);
System.out.println(obj2.getString("name") + " " + obj2.getInt("age"));
}
}
对比总结
库 | 优点 | 缺点 | 适用场景 |
---|---|---|---|
Gson | 简单、轻量、易用 | 功能不如 Jackson 强大 | 小型项目、快速开发 |
Jackson | 功能强大、性能高、企业级标准 | 依赖注解多,学习曲线稍陡 | 大型项目、流式处理、复杂对象 |
Fastjson | 简单、性能高、JSONPath支持 | 早期安全漏洞、企业项目谨慎使用 | 快速开发、性能要求高的项目 |
org.json | 最原始、轻量 | 不支持对象映射,功能简单 | 小工具、轻量操作 JSON |
整理一张完整对比表,用同一个 JSON 数据在 Gson、Jackson、Fastjson、org.json 四种库中进行 对象 → JSON、JSON → 对象、修改字段、数组操作 的示例。
JSON 数据示例
{
"name": "Alice",
"age": 25,
"emails": ["alice@example.com", "alice123@example.com"]
}
1. Gson 示例
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.List;
class User {
String name;
int age;
List<String> emails;
}
public class GsonExample {
public static void main(String[] args) {
Gson gson = new Gson();
// 对象 -> JSON
User user = new User();
user.name = "Alice";
user.age = 25;
user.emails = List.of("alice@example.com", "alice123@example.com");
String jsonStr = gson.toJson(user);
System.out.println("Gson toJson: " + jsonStr);
// JSON -> 对象
User u2 = gson.fromJson(jsonStr, User.class);
System.out.println("Gson fromJson: " + u2.name + ", " + u2.age + ", " + u2.emails);
// 修改字段
u2.age = 26;
System.out.println("Gson modified age: " + gson.toJson(u2));
// 访问数组
List<String> emails = u2.emails;
System.out.println("Gson emails: " + emails.get(0));
}
}
2. Jackson 示例
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
class User {
public String name;
public int age;
public List<String> emails;
}
public class JacksonExample {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
User user = new User();
user.name = "Alice";
user.age = 25;
user.emails = List.of("alice@example.com", "alice123@example.com");
// 对象 -> JSON
String jsonStr = mapper.writeValueAsString(user);
System.out.println("Jackson toJson: " + jsonStr);
// JSON -> 对象
User u2 = mapper.readValue(jsonStr, User.class);
System.out.println("Jackson fromJson: " + u2.name + ", " + u2.age + ", " + u2.emails);
// 修改字段
u2.age = 26;
System.out.println("Jackson modified age: " + mapper.writeValueAsString(u2));
// 访问数组
System.out.println("Jackson emails: " + u2.emails.get(0));
}
}
3. Fastjson 示例
import com.alibaba.fastjson.JSON;
import java.util.List;
class User {
public String name;
public int age;
public List<String> emails;
}
public class FastjsonExample {
public static void main(String[] args) {
User user = new User();
user.name = "Alice";
user.age = 25;
user.emails = List.of("alice@example.com", "alice123@example.com");
// 对象 -> JSON
String jsonStr = JSON.toJSONString(user);
System.out.println("Fastjson toJson: " + jsonStr);
// JSON -> 对象
User u2 = JSON.parseObject(jsonStr, User.class);
System.out.println("Fastjson fromJson: " + u2.name + ", " + u2.age + ", " + u2.emails);
// 修改字段
u2.age = 26;
System.out.println("Fastjson modified age: " + JSON.toJSONString(u2));
// 访问数组
System.out.println("Fastjson emails: " + u2.emails.get(0));
}
}
4. org.json 示例
import org.json.JSONObject;
import org.json.JSONArray;
public class OrgJsonExample {
public static void main(String[] args) {
// 构造 JSONObject
JSONObject obj = new JSONObject();
obj.put("name", "Alice");
obj.put("age", 25);
obj.put("emails", new JSONArray(List.of("alice@example.com", "alice123@example.com")));
System.out.println("org.json toJson: " + obj.toString());
// JSON -> JSONObject
JSONObject obj2 = new JSONObject(obj.toString());
System.out.println("org.json fromJson: " + obj2.getString("name") + ", " + obj2.getInt("age") + ", " + obj2.getJSONArray("emails"));
// 修改字段
obj2.put("age", 26);
System.out.println("org.json modified age: " + obj2.toString());
// 访问数组
JSONArray emails = obj2.getJSONArray("emails");
System.out.println("org.json emails: " + emails.getString(0));
}
}
✅ 对比总结
功能 | Gson | Jackson | Fastjson | org.json |
---|---|---|---|---|
对象 → JSON | toJson() |
writeValueAsString() |
JSON.toJSONString() |
JSONObject.put() → toString() |
JSON → 对象 | fromJson() |
readValue() |
JSON.parseObject() |
new JSONObject() |
修改字段 | 支持对象直接修改再序列化 | 支持对象直接修改再序列化 | 支持对象直接修改再序列化 | put() 修改字段 |
数组访问 | 对象 List 属性直接访问 | 对象 List 属性直接访问 | 对象 List 属性直接访问 | JSONArray.get() |
特点 | 轻量、简单 | 功能强大、企业级 | 快速、性能好 | 原始轻量,手动操作 |