一引入依赖
<!-- json解析的工具包 -->
<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.8.6</version>
</dependency>
<!-- 主要为了代码简洁和日志打印 -->
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version>
</dependency>
二用到的测试实体(不需要序列化和反序列化的字段用transient声明)
@Data
@AllArgsConstructor
public class Student implements Serializable,Cloneable {private static final long serialVersionUID = 6447932156561222739L;private Integer sNo;private String name;private Date birthday;private Integer classNo;private String grade;/*** 方便对象复制* @return* @throws CloneNotSupportedException*/@Overrideprotected Student clone() throws CloneNotSupportedException {return (Student) super.clone();}
}
三转json字符串(序列化)
方便使用,我在测试类将Gson设置为全局变量
private static final Gson GSON = new Gson();
Student s1 = new Student(1,"张三",new Date(),1001,"一年级");String s1Json = GSON.toJson(s1);log.info("Gson将Student转的json为{}",s1Json);
测试结果:
Gson将Student转的json为{“sNo”:1,“name”:“张三”,“birthday”:“Jun 16, 2024 11:14:35 AM”,“classNo”:1001,“grade”:“一年级”}
Student s1 = new Student(1,"张三",new Date(),1001,"一年级");
Student s2 = s1.clone();
s2.setSNo(2);
Student s3 = s1.clone();
s3.setSNo(3);
Student s4 = s1.clone();
s4.setSNo(4);
Map<String,Student> data = new LinkedHashMap();
data.put("s1",s1);
data.put("s2",s2);
data.put("s3",s3);
data.put("s4",s4);
String mapJson = GSON.toJson(data);
log.info("Gson将map转的json为{}",mapJson);
测试结果:
Gson将map转的json为{“s1”:{“sNo”:1,“name”:“张三”,“birthday”:“Jun 16, 2024 11:14:35 AM”,“classNo”:1001,“grade”:“一年级”},“s2”:{“sNo”:2,“name”:“张三”,“birthday”:“Jun 16, 2024 11:14:35 AM”,“classNo”:1001,“grade”:“一年级”},“s3”:{“sNo”:3,“name”:“张三”,“birthday”:“Jun 16, 2024 11:14:35 AM”,“classNo”:1001,“grade”:“一年级”},“s4”:{“sNo”:4,“name”:“张三”,“birthday”:“Jun 16, 2024 11:14:35 AM”,“classNo”:1001,“grade”:“一年级”}}
Student s1 = new Student(1,"张三",new Date(),1001,"一年级");
Student s2 = s1.clone();
s2.setSNo(2);
Student s3 = s1.clone();
s3.setSNo(3);
Student s4 = s1.clone();
s4.setSNo(4);
List<Student> studentList = new ArrayList<>(Arrays.asList(s1,s2,s3,s4));
String listJson = GSON.toJson(studentList);
log.info("Gson将List转的json为{}",listJson);
四json转对象(反序列化)
Student s1 = new Student(1,"张三",new Date(),1001,"一年级");
String s1Json = GSON.toJson(s1);
log.info("Gson将Student转的json为{}",s1Json);
Student json1Student = GSON.fromJson(s1Json,Student.class);
log.info("json转实体Student结果:{}",json1Student);
测试结果:
json转实体Student结果:Student(sNo=1, name=张三, birthday=Sun Jun 16 11:14:35 CST 2024, classNo=1001, grade=一年级)
Student s1 = new Student(1,"张三",new Date(),1001,"一年级");
String s1Json = GSON.toJson(s1);
Student s2 = s1.clone();
s2.setSNo(2);
Student s3 = s1.clone();
s3.setSNo(3);
Student s4 = s1.clone();
s4.setSNo(4);Map<String,Student> data = new LinkedHashMap();
data.put("s1",s1);
data.put("s2",s2);
data.put("s3",s3);
data.put("s4",s4);String mapJson = GSON.toJson(data);
log.info("Gson将map转的json为{}",mapJson);Map<String,Student> json2map = GSON.fromJson(mapJson,Map.class);
log.info("json转Map结果:{}",json2map);
测试结果:
json转Map结果:{s1={sNo=1.0, name=张三, birthday=Jun 16, 2024 11:14:35 AM, classNo=1001.0, grade=一年级}, s2={sNo=2.0, name=张三, birthday=Jun 16, 2024 11:14:35 AM, classNo=1001.0, grade=一年级}, s3={sNo=3.0, name=张三, birthday=Jun 16, 2024 11:14:35 AM, classNo=1001.0, grade=一年级}, s4={sNo=4.0, name=张三, birthday=Jun 16, 2024 11:14:35 AM, classNo=1001.0, grade=一年级}}
Student s1 = new Student(1,"张三",new Date(),1001,"一年级");
Student s2 = s1.clone();
s2.setSNo(2);
Student s3 = s1.clone();
s3.setSNo(3);
Student s4 = s1.clone();
s4.setSNo(4);List<Student> studentList = new ArrayList<>(Arrays.asList(s1,s2,s3,s4));
String listJson = GSON.toJson(studentList);
log.info("Gson将List转的json为{}",listJson);Type listType = new TypeToken<ArrayList<Student>>(){}.getType();
List<Student> json2List = GSON.fromJson(listJson,listType);
log.info("json转List结果:{}",json2List);
测试结果:
json转List结果:[Student(sNo=1, name=张三, birthday=Sun Jun 16 11:14:35 CST 2024, classNo=1001, grade=一年级), Student(sNo=2, name=张三, birthday=Sun Jun 16 11:14:35 CST 2024, classNo=1001, grade=一年级), Student(sNo=3, name=张三, birthday=Sun Jun 16 11:14:35 CST 2024, classNo=1001, grade=一年级), Student(sNo=4, name=张三, birthday=Sun Jun 16 11:14:35 CST 2024, classNo=1001, grade=一年级)]