我运行它来将一些数据模拟到我的firebase引用中:
String id = firebase.push().getKey();
FirebaseSchool school = new FirebaseSchool(id, "Test school", "Test city", "Test address");
firebase = firebase.child(id);
firebase.setValue(school);
id = firebase.child("rooms").push().getKey();
FirebaseRoomReference roomReference = new FirebaseRoomReference(id, "Test room", "Test owner");
firebase.child("rooms").child(id).setValue(roomReference);
id = firebase.child("rooms").push().getKey();
FirebaseRoomReference roomReference2 = new FirebaseRoomReference(id, "Test room 2", "Test owner 2");
firebase.child("rooms").child(id).setValue(roomReference2);
创建以下json:
"schools" : {
"-K9LoPw-o0dOGAdWYqBF" : {
"address" : "Test city",
"city" : "Test address",
"id" : "-K9LoPw-o0dOGAdWYqBF",
"name" : "Test school",
"rooms" : {
"-K9LoPwDuzW-d19432kD" : {
"id" : "-K9LoPwDuzW-d19432kD",
"name" : "Test room",
"ownerName" : "Test owner"
},
"-K9LoPwFPTrwSpUs0dZS" : {
"id" : "-K9LoPwFPTrwSpUs0dZS",
"name" : "Test room 2",
"ownerName" : "Test owner 2"
}
}
}
}
按照建议改变了Map<>到列表<>在我的回复课程中:
public class FirebaseSchool {
private String id;
private String name;
private String address;
private String city;
private List<FirebaseUserReference> admins;
private List<FirebaseRoomReference> rooms;
public List<FirebaseUserReference> getAdmins() {
return admins;
}
public void setAdmins(List<FirebaseUserReference> admins) {
this.admins = admins;
}
public List<FirebaseRoomReference> getRooms() {
return rooms;
}
public void setRooms(List<FirebaseRoomReference> rooms) {
this.rooms = rooms;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
但是当我尝试使用snapshot.getValue(FirebaseSchool.class)
阅读学校时,我收到以下错误:
com.firebase.client.FirebaseException: Failed to bounce to type
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.StringReader@dbf0e0f; line: 1, column: 44] (through reference chain: com.usehomeroom.vasuki.data.entities.firebase.FirebaseSchool["rooms"])
答案 0 :(得分:0)
看起来您的数据结构正在映射到数组,而不是映射。我对此感到有些惊讶,因为你的第一个索引是1(不是0)。但错误信息非常清楚它正在尝试做什么。
因此,要解决此问题,请将房间映射到List
或数组:
private List<FirebaseRoomReference> rooms;
为防止出现此类问题,高度建议不要使用数字索引。要么使用push()
生成的值,要么使用固定字符串作为数字的前缀,这样就不会混淆它的类型:room_1
,room_2
等。
我可以用这个来阅读你更新的JSON:
public static class FirebaseRoomReference {
private String id;
private String name;
private String ownerName;
public String getId() { return id; }
public String getName() { return name; }
public String getOwnerName() { return ownerName; }
}
public static class FirebaseSchool {
private String id;
private String name;
private String address;
private String city;
//private List<FirebaseUserReference> admins;
private Map<String, FirebaseRoomReference> rooms;
//public List<FirebaseUserReference> getAdmins() { return admins; }
public Map<String, FirebaseRoomReference> getRooms() {
return rooms;
}
public String getId() { return id; }
public String getName() { return name; }
public String getAddress() { return address; }
public String getCity() { return city; }
}
public static void main(String[] args) throws Exception {
Firebase ref = new Firebase("https://stackoverflow.firebaseio.com/35109413");
ref.child("schools").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String s) {
System.out.println(snapshot.getValue(FirebaseSchool.class));
}
});
我改变的事情:
在我改变最后一点之前,我收到了与您相同的错误消息。