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