Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Firebase - Can't receive proper JSON from Firebase snapshot

My android app connects to Firebase and pulls "Alert Objects" that are sent there by my server.

When I export the data from Firebase, I get a beautifully formated JSON representation of the data.

Problem: When I pull the data to my android device using a DataSnapshot, the data has '=' (equals signs) instead of ':' (semicolons). Also the quotations are not there.

When I try to do something like JSONObject alert = new JSONObject(data.getValue().toString()); I get errors for obvious reasons. I say obvious because if you look at what my code prints to the console you can see that it is no longer in valid JSON format.

A friend mentioned that I need to do something with encoding but we didn't have time to discuss it.

How can I iterate through these (kinda weird) Alert Objects that I have created and turn them into JSON objects within my Java so that I can access their properties like alert.date and alert.message.

I thought screenshots would help you see what I am doing. The firebase is not secured at all so you can feel free to take a look at it. It won't really do much and when I go to production I will be moving it anyways.

Firebase screenshot

JSON export straight from Firebase website

Code in Android Studio to print each object to console

Output to console when running app

I am sure this is a super easy question to answer, I am just not too well versed with JSON and encoding as a whole.

Thanks!

like image 241
Ryan Avatar asked May 07 '16 17:05

Ryan


2 Answers

you can it using gson library

       public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            /*JSONObject jsonObject = null;
            try {
                jsonObject=new JSONObject();

            } catch (JSONException e) {
                e.printStackTrace();
            }*/
            Gson gson = new Gson();
            String s1 = gson.toJson(dataSnapshot.getValue());
            JSONArray object = null;
            try {
                object = new JSONArray(s1);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            JSONArray jsonArray = object;

            Log.e("string", s1);

        }
like image 149
user3385125 Avatar answered Nov 14 '22 22:11

user3385125


You cannot access JSON natively in Java.

But the Firebase DataSnapshot class provides everything you need.

If you have a DataSnapshot of the data at the fbAlerts in your screenshot, you can print the date+message and recipients for each:

for (DataSnapshot alert: alerts.getChildren()) {
  System.out.println(alert.child("date").getValue();
  System.out.println(alert.child("message").getValue();
  for (DataSnapshot recipient: alert.child("recipients").getChildren()) {
    System.out.println(recipient.child("name").getValue();
  }
}

Alternatively, you can build a Java class that represents an alert. See the Firebase guide on reading data in Android for examples of that.

like image 44
Frank van Puffelen Avatar answered Nov 14 '22 21:11

Frank van Puffelen