Gson的常见用法

一引入依赖

xml 复制代码
<!-- 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声明)

java 复制代码
@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
     */
    @Override
    protected Student clone() throws CloneNotSupportedException {
        return (Student) super.clone();
    }
}

三转json字符串(序列化)

方便使用,我在测试类将Gson设置为全局变量

java 复制代码
private static final Gson GSON = new Gson();

对象序列化

java 复制代码
 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":"一年级"}

Map容器序列化(如果Map序列化对数据顺序有要求,可以使用LinkedHashMap)

java 复制代码
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":"一年级"}}

List容器序列化

java 复制代码
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转对象(反序列化)

json转实体,以上面Student为例

java 复制代码
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=一年级)

json转Map

java 复制代码
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=一年级}}

json转List

java 复制代码
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=一年级)]

相关推荐
智慧老师24 分钟前
Spring基础分析13-Spring Security框架
java·后端·spring
lxyzcm26 分钟前
C++23新特性解析:[[assume]]属性
java·c++·spring boot·c++23
alikami41 分钟前
【若依】用 post 请求传 json 格式的数据下载文件
前端·javascript·json
V+zmm101341 小时前
基于微信小程序的乡村政务服务系统springboot+论文源码调试讲解
java·微信小程序·小程序·毕业设计·ssm
Oneforlove_twoforjob1 小时前
【Java基础面试题025】什么是Java的Integer缓存池?
java·开发语言·缓存
xmh-sxh-13141 小时前
常用的缓存技术都有哪些
java
AiFlutter2 小时前
Flutter-底部分享弹窗(showModalBottomSheet)
java·前端·flutter
J不A秃V头A2 小时前
IntelliJ IDEA中设置激活的profile
java·intellij-idea
DARLING Zero two♡2 小时前
【优选算法】Pointer-Slice:双指针的算法切片(下)
java·数据结构·c++·算法·leetcode
小池先生3 小时前
springboot启动不了 因一个spring-boot-starter-web底下的tomcat-embed-core依赖丢失
java·spring boot·后端