Google Gson

Google Gson,第1张

一、Gson介绍
什么是Gson

Gson是Google开发的Java API,是一个简单的基于Java的开源库,用于转换Java对象和Json

Gson的特点
  • 易于使用        :Gson API提供了一个高级外观来简化常用的用例
  • 无须创建映射 :Gson API为大部分要序列化的对象提供了默认映射
  • 性能优            :Gson速度较快,内存占用量低。
  • 无依赖性        :Gson不需要JDK以外的其他库

二、Maven 依赖

  com.google.code.gson
  gson
  2.8.6


三、序列化和反序列化
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.Data;
import org.junit.Test;
import java.util.*;

public class GsonTest {
    //测试的实体类
    @Data
    public class TestEntity {
        private String name;
        private int age;
        private String sex;
        private Date birthday;
        private String email;
        private String address;

        public TestEntity() {}

        public TestEntity(String name, int age, String sex, Date birthday, String email, String address) {
            this.name = name;
            this.age = age;
            this.sex = sex;
            this.birthday = birthday;
            this.email = email;
            this.address = address;
        }
    }
    @Test
    public void gsonTset()  {
        //声明Gson对象
        Gson gson = new Gson();
        //声明Java对象,用于序列化
        TestEntity entity  = new TestEntity("盛夏 ",23,"未知",new Date(),"sx @xx.com",null);
        TestEntity entity1 = new TestEntity("盛夏1",23,"未知",new Date(),"sx1@xx.com",null);
        TestEntity entity2 = new TestEntity("盛夏2",23,"未知",new Date(),"sx2@xx.com",null);
        //序列化方法 toJson || 反序列化方法 fromJson


        /*-------序列化 对象 转化为 Json 字符串  -------*/
        String objectToJson = gson.toJson(entity);
        System.out.println(objectToJson);
        //{"name":"盛夏","age":23,"sex":"未知","birthday":"Apr 26, 2022 11:20:09 AM","email":"sx@xx.com"}
        /*-------反序列化 Json 转化为 Java 对象  -------*/
        TestEntity jsonToObject = gson.fromJson(objectToJson, TestEntity.class);
        System.out.println(jsonToObject);
        //GsonTest.TestEntity(name=盛夏 , age=23, sex=未知, birthday=Tue Apr 26 11:34:06 CST 2022, email=sx @xx.com, address=null)


        /*-------序列化 Map 转化为 Json 字符串  -------*/
        Map map = new HashMap();
        map.put("entity1",entity1);
        map.put("entity2",entity2);
        String mapToJson = gson.toJson(map);
        System.out.println(mapToJson);
        //{"entity1":{"name":"盛夏1","age":23,"sex":"未知","birthday":"Apr 26, 2022 11:28:49 AM","email":"sx1@xx.com"},
        // "entity2":{"name":"盛夏2","age":23,"sex":"未知","birthday":"Apr 26, 2022 11:28:49 AM","email":"sx2@xx.com"}}
        /*-------反序列化 Json 转化为 Map 集合  -------*/
        Map jsonToMap = gson.fromJson(mapToJson, new TypeToken>() {}.getType());
        System.out.println(jsonToMap);
        //{entity1=GsonTest.TestEntity(name=盛夏1, age=23, sex=未知, birthday=Tue Apr 26 11:38:38 CST 2022, email=sx1@xx.com, address=null),
        // entity2=GsonTest.TestEntity(name=盛夏2, age=23, sex=未知, birthday=Tue Apr 26 11:38:38 CST 2022, email=sx2@xx.com, address=null)}


        /*-------序列化 List 转化为 Json 字符串  -------*/
        List list = new ArrayList();
        list.add(entity1);
        list.add(entity2);
        String listToJson = gson.toJson(list);
        System.out.println(listToJson);
        //[{"name":"盛夏1","age":23,"sex":"未知","birthday":"Apr 26, 2022 11:31:37 AM","email":"sx1@xx.com"},
        // {"name":"盛夏2","age":23,"sex":"未知","birthday":"Apr 26, 2022 11:31:37 AM","email":"sx2@xx.com"}]
        /*-------反序列化 Json 转化为 List 集合  -------*/
        List jsonToList = gson.fromJson(listToJson, new TypeToken>() {}.getType());
        System.out.println(jsonToList);
        // [GsonTest.TestEntity(name=盛夏1, age=23, sex=未知, birthday=Tue Apr 26 11:41:01 CST 2022, email=sx1@xx.com, address=null),
        //  GsonTest.TestEntity(name=盛夏2, age=23, sex=未知, birthday=Tue Apr 26 11:41:01 CST 2022, email=sx2@xx.com, address=null)]
    }



}
四、常用注解

这个人很懒------------------------------------------------------------------------

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/737693.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存