Firebase JsonMappingException:无法从 START_ARRAY 令牌中反序列化 java.util.LinkedHashMap 的实例
Firebase JsonMappingException: Can't deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
我 运行 将一些数据模拟到我的 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<> 更改为 List<> class:
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"])
您的数据结构似乎被映射到数组,而不是映射。我对此感到有些惊讶,因为您的第一个索引是 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://Whosebug.firebaseio.com/35109413");
ref.child("schools").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String s) {
System.out.println(snapshot.getValue(FirebaseSchool.class));
}
});
我改变的东西:
- 我将 POJO 类 标记为静态,因为我将它们作为内部 类 包含在我的测试程序中。
- 我删除了用户管理员,因为他们不在您的 JSON
中
- 我将你的列表更改为地图,因为你现在使用的是字符串键
在我更改最后一点之前,我收到了与您相同的错误消息。
我 运行 将一些数据模拟到我的 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<> 更改为 List<> class:
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"])
您的数据结构似乎被映射到数组,而不是映射。我对此感到有些惊讶,因为您的第一个索引是 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://Whosebug.firebaseio.com/35109413");
ref.child("schools").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String s) {
System.out.println(snapshot.getValue(FirebaseSchool.class));
}
});
我改变的东西:
- 我将 POJO 类 标记为静态,因为我将它们作为内部 类 包含在我的测试程序中。
- 我删除了用户管理员,因为他们不在您的 JSON 中
- 我将你的列表更改为地图,因为你现在使用的是字符串键
在我更改最后一点之前,我收到了与您相同的错误消息。