Android中的DeSerializing List <object> for Firebase抛出异常:com.fasterxml.jackson.databind.JsonMappingException

时间:2016-04-08 18:55:58

标签: android serialization firebase

我有一个使用类对象列表的模型。我正在尝试从firebase查询的datasnapshot获取getvalue。但是获取com.fasterxml.jackson.databind.JsonMappingException说:无法从START_OBJECT标记中反序列化java.util.ArrayList的实例。

代码如下所示:

Key.java
@JsonIgnoreProperties(ignoreUnknown = true)
Class Key implements implements Parcelable {
    private String key;

     public Key() {
     }

     public String getkey() {
       return key;
     }

     public void setkey(String name) {
         this.key = name;
     }

     @Override
     public int describeContents() {
        return 0;
      }

      @Override
      public void writeToParcel(Parcel dest, int flags) {
         dest.writeString(this.key);
      }

      protected Key(Parcel in) {
         this.key = in.readString();
       }

      public static final Creator<Key> CREATOR = new Creator<Key>() {
          public Key createFromParcel(Parcel source) {
           return new Key(source);
       }

        public Key[] newArray(int size) {
           return new Key[size];
       }
     };       

}

Model.java
@JsonIgnoreProperties(ignoreUnknown = true)
Class Model implements implements Parcelable {
    private String Addr1;
    private String Addr2;
    private List<Key>Keys;

     public Model () {
     }

     public String getAddr1() {
       return this.Addr1;
     }
     public String getAddr2() {
       return this.Addr2;
     }

     public List<Key>getKeys(){
       return this.Keys;
     }

     public void setAddr1(String addr) {
       this.Addr1 = addr;
     }
     public void setAddr2(String addr) {
       this.Addr2 = addr;
     }

     public void setKeys(List<Key> keys){
       this.Keys = keys;
     }



     @Override
     public int describeContents() {
        return 0;
      }

      @Override
      public void writeToParcel(Parcel dest, int flags) {
         dest.writeString(this.Addr1);
         dest.writeString(this.Addr2);
         dest.writeTypedList(this.Keys);

      }

      protected Key(Parcel in) {
         this.Addr1= in.readString();
         this.Addr2 = in.readString();
         this.keys= in.createTypedArrayList(Keys.CREATOR);
       }

      public static final Creator<Model> CREATOR = new Creator<Model>() {
          public Key createFromParcel(Parcel source) {
           return new Model(source);
       }

        public Model[] newArray(int size) {
           return new Model[size];
       }
     };       

}

对象模型保存到Firebase DB中。在Firebase DB中,键结构如下所示:

"Keys" : {
    "-KEr8rtIHAIAVftmRRAp" : {
      "key" : "-KEr8rquQnMzVycKlsGm"
    }
  },

在我看来,我从firebase获取数据快照并尝试序列化Model类。

for (DataSnapshot data : dataSnapshot.getChildren()){
    Model cur= data.getValue(Model.class); 
}

在调用此异常时抛出异常。我是Java序列化的新手请指导我完成问题。

1 个答案:

答案 0 :(得分:0)

解决了它。我在模型中更改为HashMapkeys,后来我意识到了json结构。