如何从Firebase Realtime数据库访问JSON树的一个键?

时间:2021-06-29 13:18:31

I want to get the value of 'caste' key from the JSON tree in Firebase Realtime Database.See this image

我想从Firebase Realtime数据库中的JSON树中获取“种姓”键的值。看到这张图片

I have the user's unique ID and all my auth and user objects are in place. How do I get a reference to only the 'caste' key into a String variable?

我有用户的唯一ID,所有的auth和用户对象都已经就绪。我怎么才能把“种姓”的钥匙变成一个字符串变量呢?

Thanks, 如何从Firebase Realtime数据库访问JSON树的一个键?

谢谢,

2 个解决方案

#1


1  

  DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("project-android-536f3");

    mDatabase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                     for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
                             String caste = (String) messageSnapshot.child("caste").getValue();
                            }
                        }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

#2


0  

I figured it out. I never provided the unique User ID so it never got data from the database.

我想出来。我从来没有提供唯一的用户ID,所以它从来没有从数据库获得数据。

final FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference ref = database.getReference(firebaseUser.getUid()).child("caste");
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    caste = dataSnapshot.getValue().toString();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }

            });

#1


1  

  DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("project-android-536f3");

    mDatabase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                     for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
                             String caste = (String) messageSnapshot.child("caste").getValue();
                            }
                        }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

#2


0  

I figured it out. I never provided the unique User ID so it never got data from the database.

我想出来。我从来没有提供唯一的用户ID,所以它从来没有从数据库获得数据。

final FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference ref = database.getReference(firebaseUser.getUid()).child("caste");
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    caste = dataSnapshot.getValue().toString();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }

            });