我有这个模式:
public class Student {
public String name;
public School school;
}
public class School {
public int id;
public String name;
}
public class Data {
public ArrayList<Student> students;
public ArrayList<School> schools;
}
我想使用Gson序列化Data对象,并得到类似下面的内容:
{ "students": [{
"name":"name1",
"school": "1" //the id of the scool, not its entire Json
}],
"school": [{ //the entire JSON
"id" : "1",
"name": "schoolName"
}]
}
为了做到这一点,我必须使用针对学生部分的自定义序列化器,以便Gson仅打印School的id。但是对于School,我必须有一个普通的序列化器。如何只使用一个Gson对象来完成所有操作?