GSON - 特定情况下的自定义序列化程序
2022-09-01 04:09:44
我有这个模式:
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仅打印学校的ID。但对于学校来说,我必须有命名序列化器。
如何只用一个 Gson 对象完成所有操作?