在firebase firestore中获取对象内的数据?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在firebase firestore中获取对象内的数据?相关的知识,希望对你有一定的参考价值。

我在我的火力基地firestore中有以下结构

enter image description here

我想获取Form1和Form2的数据如何实现..

以下是我的尝试

 registration=  query.whereEqualTo("UID", sharedPref.getString("userId",null)).addSnapshotListener(new EventListener<QuerySnapshot>() 
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) 

                if (e != null) 

                
                for (DocumentChange documentChange : documentSnapshots.getDocumentChanges()) 


                    if (documentChange.getDocument().getData().get("mobileMenus") != null) 

                        try 
                            Log.d("order","one");
                            String notesResponse = documentChange.getDocument().getData().get("mobileMenus").toString().replace(" ", "").replace(":", "");
                            String responseNotes = notesResponse.replace("=", ":");

                            Log.d("shownotes","*****    "+responseNotes);

                            JSONObject jsonObject = new JSONObject(responseNotes);

                            isAttendance = jsonObject.getString("Attendance");
                            shared_menueditor.putString("isAttendance",isAttendance);

                            isCalender = jsonObject.getString("Calender");
                            shared_menueditor.putString("Calender",isCalender);

                            isExpenses = jsonObject.getString("Expenses");
                            shared_menueditor.putString("Expenses",isExpenses);

                            isleaves = jsonObject.getString("Leaves");
                            shared_menueditor.putString("Leaves",isleaves);

                            isLogout = jsonObject.getString("LogOut");
                            shared_menueditor.putString("LogOut",isLogout);

                            isNoticeboard = jsonObject.getString("NoticeBoard");
                            shared_menueditor.putString("NoticeBoard",isNoticeboard);

                            isTasks = jsonObject.getString("Tasks");
                            shared_menueditor.putString("Tasks",isTasks);

                            isTrackEmployee = jsonObject.getString("TrackEmployee");
                            shared_menueditor.putString("TrackEmployee",isTrackEmployee);

                           // documentChange.getDocument().getData().get("dynForms");
                           // Log.d("total", String.valueOf(documentChange.getDocument().getData().get("dynForms")));
                            Log.d("order","two");
                            forms= (Map<String, Object>) documentChange.getDocument().getData().get("mobileMenus");

                            Log.d("showfomsizes","***   "+forms.size());



                         catch (JSONException e1) 
                            e1.printStackTrace();
                        
                    


                    if (forms!= null) 
                        for (Map.Entry<String, Object> form : forms.entrySet()) 
                            String key = form.getKey();
                            Map<Object, Object> values = (Map<Object, Object>) form.getValue();
                            name = (String) values.get("name");
                            String id = (String) values.get("id");
                            Log.d("nesteddata", name + "......" + id + "......." + key);
                            if (key.contains("Form1")) 
                                shared_menueditor.putString("nav_form1",name);
                            
                            if (key.contains("Form2")) 
                                shared_menueditor.putString("nav_form2",name);
                            
                            if (key.contains("Form3")) 
                                shared_menueditor.putString("nav_form3",name);
                            
                        
                        shared_menueditor.apply();
                        shared_menueditor.commit();

                    
                    userprofile();

                



            

        );

我能够参加考勤,日历等。但是我的pbm是我无法获得Form1和Form2数据

错误:java.lang.ClassCastException:java.lang.Boolean无法强制转换为java.util.Map

答案

您正在获取该异常,因为您没有检查您拥有的布尔值。这应该是获取forms的代码段。你的forms对象是整个mobileMenus对象。

if (forms!= null) 
   for (Map.Entry<String, Object> form : forms.entrySet()) 
       String key = form.getKey();
       if(key.contains("Form")) 
           Map<Object, Object> values = (Map<Object, Object>) form.getValue();
           name = (String) values.get("name");
           String id = (String) values.get("id");
           Log.d("nesteddata", name + "......" + id + "......." + key);
           if (key.contains("Form1"))                                
               shared_menueditor.putString("nav_form1",name);
           
           if (key.contains("Form2"))                   
               shared_menueditor.putString("nav_form2",name);
           
           if (key.contains("Form3"))                              
               shared_menueditor.putString("nav_form3",name);
           
         
       

   

以上是关于在firebase firestore中获取对象内的数据?的主要内容,如果未能解决你的问题,请参考以下文章

Firestore - 删除对象内的字段

Firebase Firestore从数组Javascript查询

将 Firebase Firestore 文档值分配给 swift 变量 [重复]

@firebase/firestore:Firestore (5.0.4):无法访问 Cloud Firestore 后端。后端在 10 秒内没有响应

Firebase Firestore 读取和定价

Firebase Firestore +云功能服务器端验证应用内购买收据(Swift + NodeJS)