1

When I retrieve my data it contains brackets { and unique id such as - JSDHGJDGJJSKA ... I want to make it cleaner and get rid of the brackets for e.g. my output is:

{-JfFQQRYnhiKeuN5ERGX={msg=Monday},-JfFQAhQQWIFAUuV1nD4={msg=this is test}}

I want to get rid of the brackets and the word msg and retrieve just one of the message at random.

I want my output to be if I pick up a random message:

Monday

if I pick up another at random

this is test

Any ideas on how to achieve this will be greatly appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
nothingness
  • 694
  • 3
  • 9
  • 25
  • Read about regular expressions. – Simon Jan 09 '15 at 21:13
  • Please add two code fragments: one of how you add the data and one of how you retrieve it. – Frank van Puffelen Jan 09 '15 at 22:53
  • ok i will update after i finished lol game. – nothingness Jan 09 '15 at 23:25
  • You've posted this question twice now. In both cases, you failed to show how you are retrieving the data. You also need to read [the guide](https://www.firebase.com/docs/web/guide/) which is designed to teach these fundamentals, and to save you (and the people you are leaning on for help) a great deal of time thrashing. Additionally, pick a question. Do you want a random key from the object or an alternative to push ids? – Kato Jan 12 '15 at 18:27

3 Answers3

3

This will retrieve a random message from the object you've shown in your question.

function getRandomMessage(data) {
   if( !data ) { return null; }
   var keys = Object.keys(data);
   var randomKey = keys[ Math.floor(Math.random()*keys.length) ];
   return data[randomKey];
}

Keep in mind that this assumes you have a small number of records. If you start getting into the thousands, you'll need a more robust solution than just grabbing the entire data set.

Kato
  • 40,352
  • 6
  • 119
  • 149
1

When I used this I was able to retrieve my data for eg. I save as Book -> title: "The book of death" here is the code to retrieve the title:Retrieve data-

String title = (String) snapshot.child("title").getValue();

It worked after I used and I didnt used push since push creates its unique ID and its complex for my level to deal with it so I used:Saving data-

                Map<String, Object> title= new HashMap<String, Object>();
                title.put("title", "This is a working message");
                f.child("Book").updateChildren(title);

and everything worked out. I hope it helps everyone who has having these issues. With update children you can use auto increment for your id.

nothingness
  • 694
  • 3
  • 9
  • 25
0

are you getting data in string ? and If you are using string then it is easy , you can use the method of replace eg: yourString.replace("a","b")

Black
  • 21
  • 6
  • ah i will try i thought you must use a function from firebase thanks for the advice. will let you know if it works the way i wanted. – nothingness Jan 09 '15 at 22:36
  • I did try it but it was very difficult to manipulate especially giving the unique id by push i had to replace every single one of the unique id so its a reasonable answer but in this situation i used a different method for simpler usage see my answer below, thanks for help though. – nothingness Jan 12 '15 at 11:31