项目前后端交互采用的是JSON传输,因为前后端沟通不多,经常造成数据格式不统一;在不想后台改动太多下,需要使用@SerializedName解决这个问题
解析数据使用的是Gson解析的json数据
@SerializedName(value = "name", alternate = {"Name", "studentName", "fullName"})中有两个属性,value="默认key",alternate="别名key1,别名key1...",别名可以有多个,多个别名用“,”分隔,便降低了数据别名不统一造成的解析异常
public class Student{ @SerializedName(value = "name", alternate = {"Name", "studentName", "fullName"}) private String name; @SerializedName(value = "age", alternate = {"Age", "year", "Year"}) private String age; @SerializedName(value = "class", alternate = {"Class", "remove", "Remove"}) private String class; @SerializedName(value = "id", alternate = {"ID", "studentId", "stuengtNumber"}) private String id; public String getName() { return name; } public void setName(String name) { this.name= name; } public String getAge() { return age; } public void setAge(String age) { this.age= age; } public String getClass() { return class; } public void setClass(String class) { this.class = class; } public String getId() { return id; } public void setId(String id) { this.id= id; } }