之前,我们有以下课程public class Checklist { private final long id; private String text; private boolean checked; public Checklist(long id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } public long getId() { return id; }}以下 json 操作(字符串到对象)效果很好。String string = "[{\"checked\":false,\"id\":0,\"text\":\"Boodschappen\"},{\"checked\":false,\"id\":1,\"text\":\"Melk\"}]";GsonBuilder builder = new GsonBuilder();final Gson gson = builder.create();List<Checklist> checklists = gson.fromJson(string, new TypeToken<List<Checklist>>() {}.getType());for (Checklist checklist : checklists) { System.out.println("--> " + checklist.getText());}现在,我们将该类“升级”到以下级别,以节省磁盘空间。public class Checklist { @SerializedName("i") private final long id; @SerializedName("t") private String text; @SerializedName("c") private boolean checked; public Checklist(long id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } public long getId() { return id; }}但是,我们仍然希望能够将旧的 json 字符串转换回新的对象。String oldString = "[{\"checked\":false,\"id\":0,\"text\":\"Boodschappen\"},{\"checked\":false,\"id\":1,\"text\":\"Melk\"}]";我可以知道如何才能实现这一目标吗?
1 回答
千万里不及你
TA贡献1784条经验 获得超9个赞
您可以将替代品作为参数传递给 SerializedName
@SerializedName(value = "fullName", alternate = "username")
添加回答
举报
0/150
提交
取消