本文介绍了Firebase数据库错误 - 在反序列化时期望映射,但得到了一个类java.util.ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:想出来,检查我发布的答案,如果你有类似的问题。

我知道有几个问题这个问题,但他们的解决方案都没有为我工作。

在我的模型类中,我已经确保使用List而不是Arraylist来避免Firebase问题,但是仍然收到此错误。这是很多的代码,但大多数问题要求所有的代码,所以我会发布一切。

TemplateModelClass.java

  // 
code>

我多次成功地使用了这个基本模型。对于

$ $ p $ HashMaps< String,List< String>> ;,
pre>

字符串是一个递增的整数转换为字符串。列表只是列表中的字符串。以下是一些来自Firebase的示例JSON:

  // 

尽我所能格式化。如果你需要的照片,让我知道,我会得到一个截图



我得到这个错误,如标题所述:

  com.google.firebase.database.DatabaseException:反序列化期望映射,但得到一个类java.util.ArrayList 

关于这个的最有争议的问题似乎与使用整数作为关键的问题有关,但我想我已经通过总是使用转换为字符串的整数来避免这种情况。这可能是奇怪的解释,所以我会尝试更多的东西在此期间。感谢您的阅读!

解决方案

好的,想通了。如果有人读这个问题有这个问题,并使用递增ints / longs /无论转换为字符串,你必须添加一些字符转换为int。如果可以转换,Firebase显然会将这些键转换回非字符串。



例如,如果你做这样的事情:

  int inc = 0; 
inc ++; // 1
map.put(String.valueOf(inc),someList);

Firebase将此键解释为1而不是1。



因此,强制Fb以字符串的形式解释,像这样做:

  int inc = 0; 
inc ++; // 1
map.put(String.valueOf(inc)+_key,someList);

一切都完美无缺。很显然,如果你还需要把这些字符串读回来,只要把字符串分割成[_]就行了。

Edit: Figured it out, check my posted answer if you're having similar issues.

I know there are several questions about this issue, but none of their solutions are working for me.

In my model class I have made sure to use List instead of Arraylist to avoid Firebase issues, but am still getting this error. It's a lot of code but most questions ask for all the code so I'll post it all.

TemplateModelClass.java

//

I've used this basic model successfully many times. For the

HashMaps<String, List<String>>,

the String is an incremented Integer converted to String. The List's are just Strings in a List. Here's some sample JSON from Firebase:

 //

Formatted that as best as I could. If you need a picture of it let me know and I'll get a screenshot

And am getting this error, as stated in the title:

com.google.firebase.database.DatabaseException: Expected a Map while deserializing, but got a class java.util.ArrayList

The most upvoted question about this seems to have something to do with a problem using an integer as a key, but I think I've avoided that by always using an integer converted to a string. It may be interpreting it strangely, so I'll try some more stuff in the meantime. Thanks for reading!

解决方案

Alright, figured it out. If anyone reading this has this problem and are using incremented ints/longs/whatever that get converted to strings, you must add some characters to the converted int. Firebase apparently converts these keys back into non-Strings if it can be converted.

For example, if you do something like this:

int inc = 0;
inc++; // 1
map.put(String.valueOf(inc), someList);

Firebase interprets that key as 1 instead of "1".

So, to force Fb to intepret as a string, do something like this:

int inc = 0;
inc++; // 1
map.put(String.valueOf(inc) + "_key", someList);

And everything works out perfectly. Obviously if you also need to read those Strings back to ints, just split the string with "[_]" and you're good to go.

这篇关于Firebase数据库错误 - 在反序列化时期望映射,但得到了一个类java.util.ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 06:17