Android-GSON

gson的github开源地址: https://github.com/google/gson

Gradle引入

1
2
3
dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
}

对象的序列化与反序列化

创建Girlfriend类,并设置get方法对外开放获取变量值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Girlfriend {
private String name;
private int age;
private double height;
private double weight;
public Girlfriend(String name,int age,double height,double weight){
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public double getHeight() {
return height;
}

public double getWeight() {
return weight;
}
}

序列化

将Girlfriend类的所有属性用json数据记录

1
2
3
4
5
6
7
//  创建Girlfriend对象
Girlfriend girlfriend1 = new Girlfriend("test",18,175.0,110.0);
// Gson对象
Gson gson1 = new Gson();
// 序列化,用json数据记录girlfriend对象中的所有属性
json = gson1.toJson(girlfriend1);//json是一个String类型的全局变量
code.setText(json);

反序列化

将json数据根据Girlfriend类型反序列化,得到一个Girlfriend类型对象

1
2
3
4
5
6
7
8
9
10
//  创建Gson对象
Gson gson2 = new Gson();
// 将json数据根据Girlfriend类型反序列化
Girlfriend girlfriend2 = gson2.fromJson(json,Girlfriend.class);//json是一个String类型的全局变量
// 从反序列化的对象中取数据
String name = girlfriend2.getName();
int age = girlfriend2.getAge();
double height = girlfriend2.getHeight();
double weight = girlfriend2.getWeight();
code.setText("名字:" + name + "\n年龄:" + age + "\n身高:" + height + "\n体重:" + weight);

创建Job类,并对外开放get方法获取job类下name和salary的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Job {
private String name;
private int salary;

public Job(String name,int salary){
this.name = name;
this.salary = salary;
}

public String getName() {
return name;
}

public int getSalary() {
return salary;
}
}

在Girlfriend类中用Job定义一个变量,并设置setJob方法开放修改job值,getJob获取job值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Girlfriend {
private String name;
private int age;
private double height;
private double weight;
private Job job;//自定义类创建的变量

public Girlfriend(String name,int age,double height,double weight){
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}

// 给job赋值
public void setJob(Job job) {
this.job = job;
}

// 获取job变量
public Job getJob() {
return job;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public double getHeight() {
return height;
}

public double getWeight() {
return weight;
}
}

带自定义类变量的序列化

创建自定义类的对象,并用类对象的变量通过通过girlfriend1的setJob给job变量赋值

1
2
3
4
5
6
7
8
9
10
11
//  创建Girlfriend对象
Girlfriend girlfriend1 = new Girlfriend("test",18,175.0,110.0);
// 创建Job对象
Job job = new Job("某公司老板",100000);
// 通过girlfriend1下的setJob方法给job赋值
girlfriend1.setJob(job);
// Gson对象
Gson gson1 = new Gson();
// 序列化,用json数据记录girlfriend对象中的所有属性
json = gson1.toJson(girlfriend1);
code.setText(json);//json是一个String类型的全局变量

带自定义类变量反序列化

多了一层get而已

1
2
3
4
5
6
7
8
9
10
11
12
//  创建Gson对象
Gson gson2 = new Gson();
// 将json数据根据Girlfriend类型反序列化
Girlfriend girlfriend2 = gson2.fromJson(json,Girlfriend.class);//json是一个String类型的全局变量
// 从反序列化的对象中取数据
String name = girlfriend2.getName();//名字
int age = girlfriend2.getAge();//年龄
double height = girlfriend2.getHeight();//身高
double weight = girlfriend2.getWeight();//体重
String jobName = girlfriend2.getJob().getName();//工作名
int salary = girlfriend2.getJob().getSalary();//收入
code.setText("名字:" + name + "\n年龄:" + age + "\n身高:" + height + "\n体重:" + weight + "\n工作:" + jobName + "\n收入:" + salary);

数组的序列化与反序列化

创建一个数组

1
2
private String[] strArr = new String[]{"value1","value2","value3"};
private String json;//序列化值

序列化

1
2
3
4
5
//  创建GSON对象
Gson gson3 = new Gson();
// 对数组序列化
json = gson3.toJson(strArr);
code.setText(json);

反序列化

1
2
3
4
5
6
7
8
9
10
//  创建GSON对象
Gson gson4 = new Gson();
// 把json数组字符串反序列化
String[] arr = gson4.fromJson(json,String[].class);
// 字符串叠加
String str = "";
for (int i = 0;i < arr.length;i++){
str += arr[i] + "\n";
}
code.setText(str);

自定义类型数组序列化与反序列化

创建自定义类型数组

1
private Girlfriend[] girlfriends = new Girlfriend[3];

在方法中给数组赋值
1
2
3
girlfriends[0] = new Girlfriend("test1",19,175.5,110.5);
girlfriends[1] = new Girlfriend("test2",20,175.0,120.0);
girlfriends[2] = new Girlfriend("test3",20,165.0,100.0);

序列化

1
2
3
4
5
//  创建Gson对象
Gson gson3 = new Gson();
// 序列化
json = gson3.toJson(girlfriends);
code.setText(json);

反序列化

1
2
3
4
5
6
//  创建Gson对象
Gson gson4 = new Gson();
// 反序列化得到Girlfriend类型数组,数组中的每个值都是一个Girlfriend对象
Girlfriend[] girlfriends2 = gson4.fromJson(json,Girlfriend[].class);
// 调用它的getName
code.setText(girlfriend3.getName());

List集合的序列化与反序列化

序列化

1
2
3
4
5
//  创建Gson对象
Gson gson5 = new Gson();
// 把集合序列化为json
json = gson5.toJson(girlfriendList);
code.setText(json);

反序列化

1
2
3
4
5
6
  Gson gson6 = new Gson();
// gson提供的数据类型转换器
Type type = new TypeToken<List<Girlfriend>>(){}.getType();
// 反序列化,把Json格式的数据转换成List<AppVersion> 类型
List<Girlfriend> girlfriendList2 = gson6.fromJson(json,type);
code.setText(girlfriendList2.get(0).getName());