Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何基于某个键从firebase检索数据';嵌套json对象中的s值_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Android 如何基于某个键从firebase检索数据';嵌套json对象中的s值

Android 如何基于某个键从firebase检索数据';嵌套json对象中的s值,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我在Firebase数据库中为我的帖子条目设置了这种结构 "posts" : { "-LP-UfzuUHFJOkY0Ogb9" : { "dislikeCount" : "0", "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/privato-dfb2c.appspot.com/o/images%2F2309fdab-0d78-44e5-af1f-8d256efdf6fd.jpg?alt=media&token=

我在Firebase数据库中为我的帖子条目设置了这种结构

"posts" : {
"-LP-UfzuUHFJOkY0Ogb9" : {
  "dislikeCount" : "0",
  "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/privato-dfb2c.appspot.com/o/images%2F2309fdab-0d78-44e5-af1f-8d256efdf6fd.jpg?alt=media&token=1276804f-d48c-4e06-8982-309fd539a397",
  "likeCount" : "0",
  "postByUser" : {
    "aboutMe" : "death is inevitable",
    "countryCode" : "+91",
    "imageUrl" : "https://i.imgur.com/hAazykl.jpg",
    "mobileNumber" : "number",
    "name" : "Sid",
    "userName" : "deathcode"
  },
  "timeStamp" : 1539754082148,
  "title" : "This is cool, lowest price ever on grocers.",
  "type" : 3
}
}
现在,我需要根据json对象“postByUser”下的userName值检索特定用户的所有帖子。 我遵循某些指南来解决这个问题,但所有这些指南都使用根post项下的json键值,而不是post项下的另一个json对象

 Query query = dbRef.child("posts").orderByChild("title").equalTo("sometitle");

但是如何将其用于嵌套的json对象呢?

在使用
orderByChild()
查询及其
dataSnapshot
后,可以获得所有同级子值

此操作的代码如下所示:

//ref是对firebase数据库的正确数据库引用
ref.orderByChild(“userName”).equalTo(userNameYouWant).addListenerForSingleValueEvent(新值EventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
对于(DataSnapshot ds:DataSnapshot.getChildren()){
String aboutMe=ds.child(“aboutMe”).getValue(String.class);
}
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
}
});

您可以使用相同的方法检索其他同级节点值。

使用
orderByChild()
查询及其
dataSnapshot
后,您可以获取所有同级子值

此操作的代码如下所示:

//ref是对firebase数据库的正确数据库引用
ref.orderByChild(“userName”).equalTo(userNameYouWant).addListenerForSingleValueEvent(新值EventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
对于(DataSnapshot ds:DataSnapshot.getChildren()){
String aboutMe=ds.child(“aboutMe”).getValue(String.class);
}
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
}
});

您可以使用相同的方法检索其他同级节点值。

您可以将路径传递给
orderByChild
,以便:

Query query = dbRef.child("posts").orderByChild("postByUser/name").equalTo("deathcode");

您可以将路径传递到
orderByChild
,以便:

Query query = dbRef.child("posts").orderByChild("postByUser/name").equalTo("deathcode");

不起作用,因为帖子的子项有我没有引用的密钥,无法获取postedByUser,也无法根据该密钥筛选帖子。您不需要在数据库中拥有
posts
的子项,该代码才能起作用。@SidhantRajora,您可以在这里了解更多信息,不,它不起作用,我更改了firebase数据库的结构。不起作用,因为posts的子项具有我没有引用的密钥,并且无法通过用户获取postedByUser,也无法通过该密钥筛选post。您不需要在数据库中具有
posts
的子项,此代码才能起作用。@SidhantRajora,您可以在此处了解更多信息,不,它不起作用,所以,我更改了firebase数据库的结构。post directly没有postByUser/name作为direct Child,这是正确的。Firebase数据库查询获取引用的每个子节点,然后查找您在
orderByChild
中为该节点指定的属性。试试看。:-)post directly没有postByUser/name作为直接子项,这是正确的。Firebase数据库查询获取引用的每个子节点,然后查找您在
orderByChild
中为该节点指定的属性。试试看。:-)嘿@SidhantRajora通过单击答案旁边的V或勾号类型查找按钮,将答案标记为正确,因为这有助于堆满未来的读者,我非常感谢,干杯嘿@SidhantRajora通过单击答案旁边的V或勾号类型查找按钮,将答案标记为正确,因为这有助于堆满未来的读者,我非常感谢,干杯