-1

I am having problems with parsing data from JSON such as this

{
-KGWZ2x71KlTRF5JxLnh: {
condition: "clear sky",
date: "29 Apr 2016",
degree: "59.90360000000002",
latitude: "33.7932608",
longitude: "-118.1359034",
placeType: "Shopping Mall",
placeVisited: "Los Altos Mall",
time: "03:28",
uid: "2fe51b4a-60b9-42cf-a889-bb607a5ea9bd",
username: "kavit kaushal"
},
-KGWZ3g5KPVaRiB6BCli: {
condition: "clear sky",
date: "29 Apr 2016",
degree: "59.90360000000002",
latitude: "33.7932608",
longitude: "-118.1359034",
placeType: "Shopping Mall",
placeVisited: "Los Altos Mall",
time: "03:28",
uid: "2fe51b4a-60b9-42cf-a889-bb607a5ea9bd",
username: "kavit kaushal"
},
-KGYQfmdGEX5lsehJ1g8: {
condition: "overcast clouds",
date: "29 Apr 2016",
degree: "65.11999999999996",
latitude: "33.7933637",
longitude: "-118.135812",
placeType: "Shopping Mall",
placeVisited: "Los Altos Mall",
time: "12:11",
uid: "2fe51b4a-60b9-42cf-a889-bb607a5ea9bd",
username: "muktika bansal"
}
}

These nodes are generated when I use Firebase.push() method.Is there a way to get all the nodes and access the information. I am using java and developing a project on google app engine.

Sreehari
  • 5,621
  • 2
  • 25
  • 59
powernit
  • 119
  • 1
  • 3
  • 16
  • The [Firebase documentation](https://www.firebase.com/docs/android/guide/retrieving-data.html) also has some good examples of how to get started. – Frank van Puffelen May 03 '16 at 04:08
  • I do but in JASON parsing I need to know objects since these are random keys I don't know what to do ? – powernit May 03 '16 at 04:08
  • 1
    Why parse it in JSON if you can already retrieve it as `DataSnapshot`? – AL. May 03 '16 at 06:48
  • 1
    You're attracting downvotes because you're not showing what you've already tried. It's typically best to show the code of your efforts and point out where you're stuck. In this case, you seem to need help with the list. A collection like this in Firebase maps to a `Map` in Java. – Frank van Puffelen May 03 '16 at 13:44
  • @trigger I am working with Google app engine and using java as my language to process my data. Above data is generated from my android app and was looking for a way to get to my firebase data. REST web api seemed simple so I went that route as I can access all the data directly in one object. Thanks I'll use Map form to parse my data. – powernit May 04 '16 at 05:52

1 Answers1

1

Firebase cannot use traditional arrays with numeric keys or we'd create collisions and override take. Take this simple example:

// ref = ['foo'];
ref.push('bar');

At the same time, another client calls:

ref.push('baz');

What happens? If we're using traditional JavaScript arrays with numeric keys, both clients try to write to ref.

Whoever gets to the server last will overwrite the data that arrived first. We certainly don't want this.

Instead of using arrays, collections of ordered data in Firebase are represented by objects with autogenerated keys that look like '-KGWZ2x71KlTRF5JxLnh'.

These keys are generated automatically and encode both the timestamp and a unique client ID. This way multiple clients can write data at the same time with collisions and your data will still be returned in order.

For handling these type of keys you need to refer this link : https://www.airpair.com/firebase/posts/firebase-building-realtime-app

Here is the another stack link which wil give some reference to you : Get users by name property using Firebase

Hope this will give you some idea regarding it.

Community
  • 1
  • 1
KishuDroid
  • 5,411
  • 4
  • 30
  • 47