1 依赖

Maven:

  1. <dependency>
  2. <groupId>com.google.code.gson</groupId>
  3. <artifactId>gson</artifactId>
  4. <version>2.8.6</version>
  5. </dependency>

github地址 :https://github.com/google/gson

2 序列化与反序列化

对象序列化

    @Test
    public void test1(){
        User user = new User();
        user.setId("a1");
        user.setName("zszxz");
        user.setAge("16");
        Gson gson = new Gson();
        String userJson = gson.toJson(user);
        // {"id":"a1","name":"zszxz","age":"16"}
        System.out.println(userJson);
    }

结果:
图片.png
结论: 将对象转为json字符串

对象反序列化

    @Test
    public void test2(){
        String userStr =  "{\"id\":\"a1\",\"name\":\"zszxz\",\"age\":\"16\"}";
        Gson gson = new Gson();
        User user = gson.fromJson(userStr, User.class);
        // User(id=a1, name=zszxz, age=16)
        System.out.println(user);
    }

结果:
图片.png
结论:将字符串转为对象

数组序列化

    @Test
    public void test3(){
        String arrayStr = "[\"a\", \"b\", \"c\"]";
        Gson gson = new Gson();
        String[] abc = gson.fromJson(arrayStr, String[].class);
        Arrays.stream(abc).forEach(e -> {
            System.out.println(e);
        });
    }

结果:
图片.png
结论: 字符串数组转为数组对象

数组反序列化

    @Test
    public void test4(){
        Gson gson = new Gson();
        String[] abc = {"a","b","c"};
        String json = gson.toJson(abc);
        // ["a","b","c"]
        System.out.println(json);
    }

结果:
图片.png
结论 :将 字符串数组转为 数组字符串

TypeAdapter

将 json对象转为对象

    @Test
    public void test8() throws IOException {
        Gson gson = new Gson();
        TypeAdapter<User> typeAdapter = gson.getAdapter(User.class);
        String jsonStr =  "{\"id\":\"a1\",\"name\":\"zszxz\",\"age\":\"16\"}";
        User user = typeAdapter.fromJson(jsonStr);
        //User(id=a1, name=null, age=16)
        System.out.println(user);
    }

结果:
图片.png
将对象序列化为对象字符串

    @Test
    public void test9() throws IOException {
        Gson gson = new Gson();
        TypeAdapter<User> typeAdapter = gson.getAdapter(User.class);
        User user = new User();
        user.setId("a1");
        user.setName("zszxz");
        user.setAge("16");
        String userStr = typeAdapter.toJson(user);
        //{"id":"a1","username":"zszxz","age":"16"}
        System.out.println(userStr);
    }

结果:
图片.png

3 常用注解

@SerializedName 修改序列化名称

User对象将 name 序列名称为 username

@Data
public class User {

    private String id;

    @SerializedName("username")
    private String name;

    private String age;
}

测试代码

    @Test
    public void test1(){
        User user = new User();
        user.setId("a1");
        user.setName("zszxz");
        user.setAge("16");
        Gson gson = new Gson();
        String userJson = gson.toJson(user);
        // {"id":"a1","username":"zszxz","age":"16"}
        System.out.println(userJson);
    }

结果:
图片.png
结论 : 输出的字符串,name 字段变为 username

@Expose()注解

  • @Expose()注解 默认参与序列化和反序列化
  • @Expose(serialize = false, deserialize = false) 不参与序列化,也不参与反序列化
  • @Expose(serialize = false) 只参与反序列化
  • @Expose(deserialize = false) 只参与序列化

    4 Json对象与Json数组

    构建json对象

      @Test
      public void test5(){
          JsonObject jsonObject = new JsonObject();
          jsonObject.addProperty("name","知识追寻者");
          jsonObject.addProperty("age",18);
          // 构建json对象{"name":"知识追寻者","age":18}
          System.out.println("构建json对象" + jsonObject);
      }
    
    结果:
    图片.png

    构建json数组

      @Test
      public void test6() {
          JsonArray jsonArray = new JsonArray();
          jsonArray.add("a");
          jsonArray.add("b");
          jsonArray.add("c");
          jsonArray.add("d");
          // 构建json数组["a","b","c","d"]
          System.out.println("构建json数组" + jsonArray);
      }
    
    结果:
    图片.png

    Json对象中加入json数组

      @Test
      public void test7() {
          // json对象
          JsonObject jsonObject = new JsonObject();
          jsonObject.addProperty("name","知识追寻者");
          jsonObject.addProperty("age",18);
          // json数组
          JsonArray jsonArray = new JsonArray();
          jsonArray.add("a");
          jsonArray.add("b");
          jsonArray.add("c");
          jsonArray.add("d");
          // json对象中加入json数组
          jsonObject.add("abcd",jsonArray);
          System.out.println(jsonObject);
      }
    
    结果:
    图片.png