2016-09-26 9 views
1

나는 중포 기지에 다음과 같은 형식의 JSON을 가지고안드로이드 중포 기지 필터링 데이터

"products" : { 
    "-KS9-I-mz7k_4IqYMQdX" : { 
     "brand" : "champion", 
     "name" : "x2 style", 
     "users" : { 
     "oGNKfltdMsVAfjDN63QjjITGnhw1" : { 
      "-KSIpwMZxesepEdNN1CW" : { 
      "-KS9IQkdC0-lRYp0hzAN" : true 
      } 
     } 
     } 
    }, 
    "-KS9-IkweDZlNf_qbbrX" : { 
     "brand" : "champion", 
     "name" : "wtab 709 tab", 
     "users" : { 
     "NKfltdMsVAfjDN63QjjITGnjdjjj" : { 
     "-KSIpwMZxesepEdNN1CW" : { 
      "-KS9IQkdC0-lRYp0hzAN" : true 
      } 
     } 
     } 
    } 
} 

내가이 키 oGNKfltdMsVAfjDN63QjjITGnhw1이있는 모든 사용자를 얻을 필요가있다. 키, 값 및 하위에 대한 필터링을 시도했습니다. 그러나 nothings는 작동합니다.

+0

이미 시도 했으므로 작동하지 않는 코드를 공유 할 수 있습니까? 적어도 우리가 사용했던 기술을 보여줄 것이지만, 우리가 쉽게 대답 할 수도 있습니다. –

답변

0

는 데이터의 스냅 샷을 반복의 예를 들어 문서를 참조하십시오 : 여기

myTopPostsQuery.addListenerForSingleValueEvent(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) { 
      // TODO: handle the post 
     } 
    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
     // Getting Post failed, log a message 
     Log.w(TAG, "loadPost:onCancelled", databaseError.toException()); 
     // ... 
    } 
}); 

그 예를 다시이지만, 이번에는 내가 더 나은 솔루션을 설명하기 위해 코드를 사용하려고 너 :

mDatabase.child("products").addListenerForSingleValueEvent(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshotLevel1) { 
     for(DataSnapshot dataSnapshotLevel2: dataSnapshotLevel1.getChildren()){ 
      for(DataSnapshot dataSnapshotLevel3: dataSnapshotLevel2.getChildren()){ 
       if(child.getKey().equals("users")){ 
        for(DataSnapshot deeperData: data.getChildren()){ 
         if(child.getKey().equals("oGNKfltdMsVAfjDN63QjjITGnhw1")){ 
          //Here is where you do whatever you need to do now that you found your child. 
         } 
        } 
       } 
      } 
     } 
    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
     Log.w(TAG, "getUser:onCancelled", databaseError.toException()); 
     // ... 
    } 
}); 
관련 문제