1 依赖
Maven:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</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);
}
对象反序列化
@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);
}
数组序列化
@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);
});
}
数组反序列化
@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);
}
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);
}
结果:
将对象序列化为对象字符串
@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);
}
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);
}
结果:
结论 : 输出的字符串,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); }
构建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); }
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); }