如何从具有嵌套子项的Firebase中检索json?

时间:2019-07-03 07:47:36

标签: java android firebase firebase-realtime-database

我已将json文件导入到Firebase实时数据库中,您可以按照屏幕快照进行操作 https://ibb.co/wyLDPC8“数据库结构”

1 个答案:

答案 0 :(得分:0)

您可以通过这种方式完成

 DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();   
 dbRef.child("categories").addSingleValueEventListener(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot dataSnapshot) {
       if (dataSnapshot.exists()) {
           for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                String name = snapshot.child("names").getValue().toString();  // it will give names node string

             /// To Fetch Quotes
             DataSnapshot quotesSnapshot = snapshot.child("qoutes");
            for (DataSnapshot quoteSingleSnpashot : quotesSnapshot.getChildren()) {
                 String quote = quoteSingleSnpashot.child("quote").getValue().toString();  // it will give names node string
            }

           }
       }
   }

   @Override
   public void onCancelled(DatabaseError databaseError) {

   }

 });
}