Firebase JsonMappingException:无法从START

编程入门 行业动态 更新时间:2024-10-23 09:28:28
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 <>:

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"])

I run this to mock some data into my firebase reference:

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);

Which creates the following 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" } } } }

as suggested I changed the Map<> to a List<> in my response 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; } }

but when I try to read a school using snapshot.getValue(FirebaseSchool.class) I get the following error:

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://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)); } });

我改变的事情:

我将POJO类标记为静态,因为我将它们作为内部类包含在我的测试程序中。 我删除了用户管理员,因为它们不在您的JSON中 我将List更改为Map,因为您现在使用的是字符串键

在我更改最后一点之前,我收到了您所做的相同错误消息。

It looks like your data structure is being mapped to an array, not a map. I'm somewhat surprised about that, since your first index is 1 (not 0). But the error message is pretty explicit about what it's trying to do.

So to solve the problem, map the rooms to an List or array:

private List<FirebaseRoomReference> rooms;

To prevent this type of problem it is highly recommended to not use numeric indexes. Either use the values generated by push() or prefix your numbers with a fixed string, so that there's never any confusion about what type it is: room_1, room_2, etc.

Update

I can read your updated JSON with this:

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)); } });

Things I changed:

I marked the POJO classes as static, since I include them as inner classes in my test program. I removed the user admins, since they're not present in your JSON I changed your List to a Map, since you're now using string keys

Before I change that last point, I got the same error message you did.

更多推荐

本文发布于:2023-07-16 15:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1130505.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Firebase   JsonMappingException   START

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!