本文介绍了如何在Realtime Firebase数据库中获取父节点的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将数据推送到JSON格式的RealTime Firebase数据库.因此,如果JSONObject在实时数据库中具有另一个JSONObject,那么如何获取该JSONObject的键"名称.

RealTime Firebase Database structured in JSON format when data is pushed in it. So if JSONObject has another JSONObject in RealTime Database, then how to get the "key" name of that JSONObject.

下面的图像是Firebase Realtime Database的结构,我想从该结构中获取密钥"January"和密钥"2018-01-23",我该怎么做.

Given below image is the structure of Firebase Realtime Database where I want to fetch the key "January" and key "2018-01-23" from structure, how can I do it.

我通过传递"Neeraj"来尝试Firebase的侦听器,因为它对我而言是众所周知的,但是我无法获取JSON结构的下一个关键名称是什么.

I tried listeners of the Firebase by passing "Neeraj" because it is known to me, but I am getting fail to fetch what is the next key name of JSON structure.

推荐答案

假定 Neeraj 节点是Firebase根的直接子级,要获取该数据,请使用以下代码:

Assuming that Neeraj node is a direct child of your Firebase-root, to get that data, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference dateRef = rootRef.child("Neeraj").child("January").child("2018-01-22");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String inStatus = dataSnapshot.child("inStatus").getValue(String.class);
        String inTime = dataSnapshot.child("inTime").getValue(String.class);
        String outSatus = dataSnapshot.child("outSatus").getValue(String.class);
        String outTime = dataSnapshot.child("outTime").getValue(String.class);
        Log.d("TAG", "Neeraj" + " / " + dataSnapshot.getRef().getParent().getKey() + " / " + dataSnapshot.getKey());
        Log.d("TAG", inStatus + " / " + inTime + " / " + outSatus + " / " + outTime);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
dateRef.addListenerForSingleValueEvent(eventListener);

您的输出将是:

1 / 11:58:12 pm / 1 / 11:58:12 pm

这篇关于如何在Realtime Firebase数据库中获取父节点的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 07:44