文章預覽
- 前言
- 操作
-
- 1、導入依賴
- 2、JavaBean
-
- 2.1、user.java
- 2.2、Job.java
- 3、對Java對象進行序列化與反序列化
- 4、對Aarray、List進行序列化與反序列化
- 5、對Map、Set集合進行序列化與反序列化
- 注解解釋
前言
GSON彌補了JSON的許多不足的地方,在實際應用中更加适用于Java開發。在這裡,我們主要講解的是利用GSON來操作java對象和json資料之間的互相轉換,包括了常見的對象序列化和反序列化的知識。
參考文章
https://www.jianshu.com/p/75a50aa0cad1
https://www.yiibai.com/gson/gson_overview.html
操作
1、導入依賴
2、JavaBean
2.1、user.java
package com.enjoy.gsondemo.bean;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class User {
@Expose
private String userName;
@Expose
private String password;
@Expose
private int age;
@Expose
private boolean isStudent;
@Expose
private Job job;
//serialize:是否參與序列化,deserialize是否參與反序列化
@Expose(serialize = false, deserialize = false)
private int test1;
private transient int test2;
@Expose
//無法以class作為字段名
@SerializedName("class")
private int cls;
public User(String userName, String password, int age, boolean isStudent) {
this.userName = userName;
this.password = password;
this.age = age;
this.isStudent = isStudent;
}
public void setTest1(int test1) {
this.test1 = test1;
}
public void setTest2(int test2) {
this.test2 = test2;
}
public void setCls(int cls) {
this.cls = cls;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
public int getAge() {
return age;
}
public boolean isStudent() {
return isStudent;
}
public Job getJob() {
return job;
}
public void setJob(Job job) {
this.job = job;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", password='" + password + '\'' +
", age=" + age +
", isStudent=" + isStudent +
", job=" + job +
", test1=" + test1 +
", test2=" + test2 +
", cls=" + cls +
'}';
}
}
2.2、Job.java
package com.enjoy.gsondemo.bean;
public class Job {
private String name;
private int salary;
public Job(String name, int salary) {
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "Job{" +
"name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
3、對Java對象進行序列化與反序列化
@Test
public void testNestedObject() {
//java對象
User u1 = new User("zzuli", "123", 18, false);
Job job = new Job("勞工", 10000);
u1.setJob(job);
//Gson提供的Gson對象
Gson gson = new Gson();
//序列化
String json = gson.toJson(u1);
System.out.println("序列化:" + json);
User u2 = gson.fromJson(json, User.class);
System.out.println("反序列化:" + u2.getUserName() + "-" + u2.getPassword() +"-"+u2.getJob());
}
4、對Aarray、List進行序列化與反序列化
package com.enjoy.gsondemo;
import com.enjoy.gsondemo.bean.User;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.junit.Test;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class ArrayUnitTest {
@Test
public void testArray() {
User[] users1 = new User[3];
//java對象
users1[0] = new User("Lance", "123", 18, false);
users1[1] = new User("Alex", "123", 88, true);
//Gson提供的Gson對象
Gson gson = new Gson();
//序列化
String json = gson.toJson(users1);
System.out.println("序列化:"+json);
User[] users2 = gson.fromJson(json, User[].class);
System.out.println("反序列化0:"+users2[0]);
System.out.println("反序列化1:"+users2[1]);
System.out.println("反序列化2:"+users2[2]);
}
@Test
public void testListObject() {
List<User> list1 = new ArrayList<>();
list1.add(new User("Lance", "123", 18, false));
list1.add(new User("Alex", "123", 88, true));
list1.add(null);
//Gson提供的Gson對象
Gson gson = new Gson();
//序列化
String json = gson.toJson(list1);
System.out.println("序列化:"+json);
//反序列化
Type type = new TypeToken<List<User>>() {
}.getType();
List<User> list2 = gson.fromJson(json, type);
System.out.println("反序列化0:"+list2.get(0).getUserName());
System.out.println("反序列化1:"+list2.get(1));
System.out.println("反序列化2:"+list2.get(2));
}
}
Array
List
5、對Map、Set集合進行序列化與反序列化
package com.enjoy.gsondemo;
import com.enjoy.gsondemo.bean.User;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.junit.Test;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class MapUnitTest {
@Test
public void testMap() {
Map<String, User> map1 = new HashMap<>();
//java對象
map1.put("1", new User("Lance", "123", 18, false));
map1.put("2", new User("Alex", "123", 88, true));
map1.put("3", null);
map1.put(null, null);
//Gson提供的Gson對象
Gson gson = new Gson();
//序列化
String json = gson.toJson(map1);
System.out.println(json);
Type type = new TypeToken<Map<String, User>>() {
}.getType();
Map<String, User> map2 = gson.fromJson(json, type);
System.out.println(map2.get(null));
System.out.println(map2.get("1"));
}
@Test
public void testSet() {
Set<User> set1 = new HashSet<>();
set1.add(new User("Lance", "123", 18, false));
set1.add(new User("Alex", "123", 88, true));
set1.add(null);
//Gson提供的Gson對象
Gson gson = new Gson();
//序列化
String json = gson.toJson(set1);
System.out.println(json);
//反序列化
Type type = new TypeToken<Set<User>>() {
}.getType();
Set<User> set2 = gson.fromJson(json, type);
Iterator<User> iterator = set2.iterator();
while (iterator.hasNext()) {
User next = iterator.next();
System.out.println("反序列化:" + next);
}
}
}
Map
Set
注解解釋
@SerializedName
注解
用于手動指定序列化的key值
@SerializedName("money")
private String salary;
@SerializedName({"money", "salary"}) // 可以有多個備選值
private String salary;
@Expose()
注解
果想要讓java類的某些字段不參加序列化或反序列化,可以顯示來設定。如:
@Expose(serialize=false,deserialize=false)
private String name;
注意使用該注解進行序列化與反序列化需要一些列方式建立Gson對象
//Gson提供的Gson對象
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();