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 girlfriend1 = new Girlfriend("test",18,175.0,110.0);
Gson gson1 = new Gson();
json = gson1.toJson(girlfriend1); code.setText(json);
|
反序列化
将json数据根据Girlfriend类型反序列化,得到一个Girlfriend类型对象
1 2 3 4 5 6 7 8 9 10
| Gson gson2 = new Gson();
Girlfriend girlfriend2 = gson2.fromJson(json,Girlfriend.class);
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; }
public void setJob(Job job) { this.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 girlfriend1 = new Girlfriend("test",18,175.0,110.0);
Job job = new Job("某公司老板",100000);
girlfriend1.setJob(job);
Gson gson1 = new Gson();
json = gson1.toJson(girlfriend1); code.setText(json);
|
带自定义类变量反序列化
多了一层get而已
1 2 3 4 5 6 7 8 9 10 11 12
| Gson gson2 = new Gson();
Girlfriend girlfriend2 = gson2.fromJson(json,Girlfriend.class);
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 gson3 = new Gson();
json = gson3.toJson(strArr); code.setText(json);
|
反序列化
1 2 3 4 5 6 7 8 9 10
| Gson gson4 = new Gson();
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 gson3 = new Gson();
json = gson3.toJson(girlfriends); code.setText(json);
|
反序列化
1 2 3 4 5 6
| Gson gson4 = new Gson();
Girlfriend[] girlfriends2 = gson4.fromJson(json,Girlfriend[].class);
code.setText(girlfriend3.getName());
|
List集合的序列化与反序列化
序列化
1 2 3 4 5
| Gson gson5 = new Gson();
json = gson5.toJson(girlfriendList); code.setText(json);
|
反序列化
1 2 3 4 5 6
| Gson gson6 = new Gson();
Type type = new TypeToken<List<Girlfriend>>(){}.getType();
List<Girlfriend> girlfriendList2 = gson6.fromJson(json,type); code.setText(girlfriendList2.get(0).getName());
|