public class A{ private int id; pirvate Date createTime private B b; }
public class B{ private int id; private String name; private Date createTime; }
此時,如果用google json組件返回A對象時,會報declares multiple JSON fields named
異常,出現這個異常是因為,兩個類都有createTime同名屬性。
解決方案:
public class SomeClassWithFields { @SerializedName("name") private final String someField; private final String someOtherField; public SomeClassWithFields(String a, String b) { this.someField = a; this.someOtherField = b; } }
===== OUTPUT =====
{"name":"a","someOtherField":"b"}
http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/annotations/SerializedName.html
