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

相关文章:

  • 2021-08-29
  • 2021-06-09
  • 2022-12-23
  • 2022-01-02
  • 2021-10-16
  • 2022-12-23
  • 2022-03-03
猜你喜欢
  • 2021-12-27
  • 2022-12-23
  • 2021-08-15
  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案