后台对象,使用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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM