如何将 Firebase 数据转换为 Java 对象...? [英] How to Convert Firebase data to Java Object...?

查看:21
本文介绍了如何将 Firebase 数据转换为 Java 对象...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Firebase 库以 Message(String, String) 的形式向服务器发送数据,添加到 HashMap

Using Firebase Library to send data to the server in the form Message(String, String) added to the HashMap<String, Message>

示例:

Firebase fb = new Firebase(URL);
Firebase msgRef = fb.child("finished");
HashMap<String, Message> msgList = new HashMap<>();
Message msg = new Message(m, n);
msgList.put(HASHKEY, msg);
msgRef.push().setValue(msgList);

使用 Firebase 方法接收数据时 addValueEventListener() 在此表单中获取 String

While receiving data with Firebase method addValueEventListener() getting String in this Form

{ key = finished, value = {
    -Js9Rn0uttjYIGdcv8I1={Moosa={message=email, name=Kamran}},
    -Js9Vsmv6BnVzOzpl2L8={Moosa={message=msgs, name=Imran}}, 
    -Js9WtQ8yeDwVxQMFCZb={Moosa={message=samsung, name=Samad}}, 
    -Js9RarxoJPKn4RO2HaM={Moosa={message=Message, name=Moosa}}, 
    -Js9b6f75lwwbsqQNJz0={Moosa={message=Qmobile, name=Bilal}}, 
    -Js9aDxt8TlgTGUccuxu={Moosa={message=last, name=Moosa}}} }

如何将其转换为消息对象.....????

How can I convert it into Message Object.....????

推荐答案

还有两种方法可以从 Firebase DataSnapshot 中获取数据,它们不需要使用 Map<字符串、对象>.

There are two more way to get your data out of the Firebase DataSnapshot that don't require using a Map<String, Object>.

第一个方法是使用DataSnapshot的方法遍历子节点:

First appoach is to use the methods of DataSnapshot to traverse the children:

ref = FirebaseDatabase.getInstance().getReference("messages").limitToLast(10);
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
            String name = (String) messageSnapshot.child("name").getValue();
            String message = (String) messageSnapshot.child("message").getValue();
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) { }
});

在上面的代码片段中,我们使用 getChildren() 来获取您的消息的 Iterable.然后我们使用 child("name") 来获取每个特定的子属性.

In the above snippet we use getChildren() to get an Iterable of your messages. Then we use child("name") to get each specific child property.

第二种方法是使用内置的 JSON-to-POJO 序列化器/反序列化器.当您发送消息列表时,其中的 Message 对象会被序列化为 JSON 并存储在 Firebase 中.

The second approach is to use the built-in JSON-to-POJO serializer/deserializer. When you're sending the message list, the Message objects inside it are serialized to JSON and stored in Firebase.

要让他们再次摆脱它,你必须做相反的事情:

To get them out of it again, you have to do the inverse:

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
            Message message = messageSnapshot.getValue(Message.class);
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) { }
});

在第二个片段中,我们仍然使用 getChildren() 来获取消息,但现在我们将它们从 JSON 直接反序列化回 Message 对象.

In this second snippet, we're still using getChildren() to get at the messages, but now we deserialize them from JSON straight back into a Message object.

有关使用最后一种方法的简单示例应用程序,请查看Firebase 的 AndroidChat 示例.它还展示了如何有效地处理消息列表(提示:FirebaseListAdapter).

For a simple sample application using that last approach, have a look at Firebase's AndroidChat sample. It also shows how to efficiently deal with the list of messages (hint: FirebaseListAdapter).

这篇关于如何将 Firebase 数据转换为 Java 对象...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆