后台对象,使用google json组件用json格式返回给前端的问题

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
原文地址:https://www.cnblogs.com/dingchenghong/p/2840076.html