是一种轻量级的数据交换格式。易于人阅读和编写,同时也易于机器解析和生成。
是Google提供的用来在java对象和JSON数据之间进行映射的java类库。可以将一个JSON字符串转成java对象。(反序列化)或者反过来(序列化)。gson可以运行于java项目而android本身就是java项目,因此Android也能使用gson。
implementation 'com.google.code.gson:gson:2.8.6'
我们在使用android测试我们写的代码的时候,在src下面的test目录里面编写。同样我们可以看到src下面还有一个androidTest目录。androidTest安卓单元测试是运行到手机里面。main目录里面的代码是需要打包成apk然后运行到虚拟机上的,运行的是Android的手机的环境。test目录里面是在电脑上测试,运行的是电脑的环境是Java的环境。因此为了方便,小编只在java测试环境里面测试代码节省打包时间。
public class User {
@Expose
private String userName;
@Expose
private String password;
@Expose
private int age;
@Expose
private boolean isStudent;
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
public int getAge() {
return age;
}
public boolean isStudent() {
return isStudent;
}
public User(String userName, String password, int age, boolean isStudent) {
this.userName = userName;
this.password = password;
this.age = age;
this.isStudent = isStudent;
}
}
public class ObjectUnitTest {
@Test
public void testObject(){
//java对象
User user1 = new User("Andlin", "123", 23, false);
//创建Goson提供的gson对象
Gson gson = new Gson();
//序列化
String json = gson.toJson(user1);
System.out.println("序列化"+json);
}
}
序列化是指:利用属性名作为key,属性值作为value记录到json数据里面
public class ObjectUnitTest {
@Test
public void testObject(){
//java对象
User user1 = new User("Andlin", "123", 23, false);
//创建Goson提供的gson对象
Gson gson = new Gson();
//序列化
String json = gson.toJson(user1);
System.out.println("序列化"+json);
//反序列化
User user2 = gson.fromJson(json, User.class);
System.out.println("反序列化"+user2.getUserName()+user2.getAge()+user2.getPassword()+user2.isStudent());
}
}
public class User {
@Expose
private String userName;
@Expose
private String password;
@Expose
private int age;
@Expose
private boolean isStudent;
@Expose
private com.example.simplenotebook.xuliehuahefanxuliehua.bean.Job job;
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
public int getAge() {
return age;
}
public boolean isStudent() {
return isStudent;
}
public void setJob(Job job) {
this.job = job;
}
public Job getJob() {
return job;
}
public User(String userName, String password, int age, boolean isStudent) {
this.userName = userName;
this.password = password;
this.age = age;
this.isStudent = isStudent;
}
}
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 +
'}';
}
}
@Test
public void testNestedObject() {
//java对象
User user1 = new User("Anglin", "123", 23, false);
Job job = new Job("工人", 1000);
user1.setJob(job);
//创建Gson提供的Gson对象
Gson gson = new Gson();
//序列化
String json = gson.toJson(user1);
System.out.println("序列化" + json);
//反序列化
User user2 = gson.fromJson(json, User.class);
System.out.println("反序列化" + user2.getUserName() + user2.getAge() + user2.getPassword() + user2.isStudent()+user2.getJob());
}
无论是多少嵌套只需要使用toJson和fromJson方法就可以了。