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=一年级)]

相关推荐
customer081 分钟前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
全栈开发圈3 分钟前
新书速览|Java网络爬虫精解与实践
java·开发语言·爬虫
WaaTong6 分钟前
《重学Java设计模式》之 单例模式
java·单例模式·设计模式
面试鸭8 分钟前
离谱!买个人信息买到网安公司头上???
java·开发语言·职场和发展
沈询-阿里1 小时前
java-智能识别车牌号_基于spring ai和开源国产大模型_qwen vl
java·开发语言
AaVictory.1 小时前
Android 开发 Java中 list实现 按照时间格式 yyyy-MM-dd HH:mm 顺序
android·java·list
哟哟耶耶1 小时前
js-将JavaScript对象或值转换为JSON字符串 JSON.stringify(this.SelectDataListCourse)
前端·javascript·json
LuckyLay1 小时前
Spring学习笔记_27——@EnableLoadTimeWeaving
java·spring boot·spring
向阳12182 小时前
Dubbo负载均衡
java·运维·负载均衡·dubbo