知识追寻者之前使用的是fastjson,感觉gson不错,抽空学了下!!
依赖
Gradle:
dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
}
Maven:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
github地址 :https://github.com/google/gson
序列化与反序列化
对象序列化
/**
* @author lsc
* <p>对象序列化 </p>
*/
@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);
}
结论; 将对象转为json字符串;
对象反序列化
/**
* @author lsc
* <p>对象反序列化 </p>
*/
@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);
}
结论:将json字符串转为对象;
数组序列化
/**
* @author lsc
* <p>数组反序列 </p>
*/
@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);
});
}
输出
a
b
c
结论: 字符串数组转为数组对象;
数组反序列化
/**
* @author lsc
* <p>数组反序列 </p>
*/
@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);
}
常用注解
@SerializedName
修改序列化名称;
User对象将 name 序列名称为 username
;
/**
* @author lsc
* <p> </p>
*/
@Data
public class User {
private String id;
@SerializedName("username")
private String name;
private String age;
}
测试代码
/**
* @author lsc
* <p>对象序列化 </p>
*/
@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) 只参与序列化
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);
}
输出
{"name":"知识追寻者","age":18,"abcd":["a","b","c","d"]}
个人站点地址:https://zszxz.com/index