@[TOC]
- 提前说结果,可以深度转换对象和数组JSON字符串。
1 依赖
<!-- 阿里JSON解析器 --> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.25</version> </dependency>
2 实体类
Person类
/** * @Author:胡云峰 * @Date:2023/4/14 16:27 */ public class Person { private String name; private int age; private String sex; private Date birthDate; private String avatar; private String car; private List<Pet> petList; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public String getAvatar() { return avatar; } public void setAvatar(String avatar) { this.avatar = avatar; } public List<Pet> getPetList() { return petList; } public void setPetList(List<Pet> petList) { this.petList = petList; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getCar() { return car; } public void setCar(String car) { this.car = car; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + ", birthDate=" + birthDate + ", avatar='" + avatar + '\'' + ", car='" + car + '\'' + ", petList=" + petList + '}'; } }
Car 类
/** * @Author:胡云峰 * @Date:2023/4/20 15:36 */ public class Car { private String carName; private String type; public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public String toString() { return "Car{" + "carName='" + carName + '\'' + ", type='" + type + '\'' + '}'; } }
Pet 类
/** * @Author:胡云峰 * @Date:2023/4/14 16:33 */ public class Pet { private String petName; private String category; public Pet() { } public Pet(String petName, String category) { this.petName = petName; this.category = category; } public String getPetName() { return petName; } public void setPetName(String petName) { this.petName = petName; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } @Override public String toString() { return "Pet{" + "petName='" + petName + '\'' + ", category='" + category + '\'' + '}'; } }
3 测试用例
- 单个对象
{ "name": "张三", "age": 16, "sex": "男" }
- 数组对象
[ { "name": "张三", "age": 16, "sex": "男" }, { "name": "李四", "age": 18, "sex": "女" } ]
- 对象包含对象
{ "name": "李四", "age": 18, "sex": "女", "car": { "carName": "桑塔纳", "type": "大众" } }
- 对象包含数组
{ "name": "李四", "age": 18, "sex": "女", "car": { "carName": "桑塔纳", "type": "大众" }, "petList": [ { "petName": "乐乐", "category": "狗" }, { "petName": "欢欢", "category": "猫" } ] }
4 测试
```java
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONReader;
import com.yunfeng.generatepdfweb.domain.Person;
import java.util.List;
/**
- @Author:胡云峰
@Date:2023/4/20 15:09
*/
public class TestFastJson2 {/**
单个对象
*/
private static final String jsonObjectStr = "{\"name\": \"张三\",\"age\": 16,\"sex\": \"男\"}";/**
对象数组
*/
private static final String jsonArrayStr = "[\n" +"{\"name\": \"张三\",\"age\": 16,\"sex\": \"男\"},\n" + "{\"name\": \"李四\",\"age\": 18,\"sex\": \"女\"}\n" + "]";
/**
测试深度转换对象
*/
private static final String deepCopyObject = "{\n" +" \"name\": \"张三\",\n" + " \"age\": 16,\n" + " \"sex\": \"男\",\n" + " \"car\": {\n" + " \"carName\": \"桑塔纳\",\n" + " \"type\": \"大众\"\n" + " }\n" + "}";
/**
测试深度转换数组对象
*/
private static final String deepCopyList = "{\n" +" \"name\": \"张三\",\n" + " \"age\": 16,\n" + " \"sex\": \"男\",\n" + " \"car\": {\n" + " \"carName\": \"桑塔纳\",\n" + " \"type\": \"大众\"\n" + " },\n" + " \"petList\": [\n" + " {\n" + " \"petName\": \"乐乐\",\n" + " \"category\": \"狗\"\n" + " }, \n" + " {\n" + " \"petName\": \"欢欢\",\n" + " \"category\": \"猫\"\n" + " }\n" + " ]\n" + "}";
public static void main(String[] args) {
// 1、单个对象的转换,自动忽略不匹配的
// 方式一:字符串转换为JSONObject,JSONObject再转换为目标对象(Person)
JSONObject jsonObject = JSONObject.parseObject(jsonObjectStr);
Person person = jsonObject.toJavaObject(Person.class, JSONReader.Feature.IgnoreAutoTypeNotMatch);
System.out.println("person = " + person);
// 方式二:直接字符串转换为目标对象(Person)
Person person1 = JSONObject.parseObject(jsonObjectStr, Person.class);
System.out.println("person1 = " + person1);// 2、数组对象的转换,自动忽略不匹配的
// 方式一:字符串转换为JSONArray,JSONArray再转换为目标list
JSONArray jsonArray = JSONArray.parse(jsonArrayStr);
List personList = jsonArray.toJavaList(Person.class, JSONReader.Feature.IgnoreAutoTypeNotMatch);
System.out.println("personList = " + personList);
// 方式二:直接字符串转化为目标list
List personList1 = JSONArray.parseArray(jsonArrayStr, Person.class);
System.out.println("personList1 = " + personList1);// 3、测试是否是深度转换
Person deepPerson = JSONObject.parseObject(deepCopyObject, Person.class);
// 结果显示对象可以深度转换对象
System.out.println("deepPerson = " + deepPerson);// 4、测试List是否可以深度转换
Person deepPerson2 = JSONObject.parseObject(deepCopyList, Person.class);
// 结果显示对象可以深度转换List
System.out.println("deepPerson2 = " + deepPerson2);
}
}# 5 结果 ```powershell person = Person{name='张三', age=16, sex='男', birthDate=null, avatar='null', car='null', petList=null} person1 = Person{name='张三', age=16, sex='男', birthDate=null, avatar='null', car='null', petList=null} personList = [Person{name='张三', age=16, sex='男', birthDate=null, avatar='null', car='null', petList=null}, Person{name='李四', age=18, sex='女', birthDate=null, avatar='null', car='null', petList=null}] personList1 = [Person{name='张三', age=16, sex='男', birthDate=null, avatar='null', car='null', petList=null}, Person{name='李四', age=18, sex='女', birthDate=null, avatar='null', car='null', petList=null}] deepPerson = Person{name='张三', age=16, sex='男', birthDate=null, avatar='null', car='{"carName":"桑塔纳","type":"大众"}', petList=null} deepPerson2 = Person{name='张三', age=16, sex='男', birthDate=null, avatar='null', car='{"carName":"桑塔纳","type":"大众"}', petList=[Pet{petName='乐乐', category='狗'}, Pet{petName='欢欢', category='猫'}]}