JeCuRo JeCuRo - 11 days ago 12
Android Question

Getting JsonArray from FireBase

I have this JSON structure on my FireBase Database:

{
"companies": [{
"name": "Soft",
"position": "Developer",
"location": "NY",
"start": 1462060800,
"end": 1470009600,
"description" : ""},
{
"name": "Xpert",
"position": "Developer",
"location": "London",
"start": 1456790400,
"end": 1462060800,
"description" : ""},
{
"name": "AXinformation",
"position": "Developer",
"location": "Paris",
"start": 1388534400,
"end": 1456790400,
"description" : " "},
.....
.....
.....


I am trying to get that data doing something like on my code

public void onCallingFireBaseExperience() {
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myRef = database.getReference("companies");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
Log.d(Constans.LOG, dataSnapshot1.child("name").getValue().toString())
}

}

@Override
public void onCancelled(DatabaseError error) {
Log.d(Constans.LOG, "Failed to read value.", error.toException());

}
});
}


But I am always facing a memory location on my LogCat


.google.firebase.database.DataSnapshot$1@f264efd


What I am doing wrong? How I can iterate the data?

Answer

It is because Firebase doesn't support storing arrays natively,

Firebase has no native support for arrays. If you store an array, it really gets stored as an "object" with integers as the key names.

Please check this link from the official Firebase blog to get more information on why arrays are not supported and how you should properly structure your data to make up for it.

So, basically the way you have structured your data is not correct according to Firebase, because of which dataSnapshot.getChildren() doesn't give you any children at all.