Android Firebase - cannot get the correct JSON from a Firebase snapshot

My Android application connects to Firebase and pulls out “Alert Objects” that are sent to my server.

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

Problem: When I pull data to my Android device using DataSnapshot, the data has "=" (equal to signs) instead of ":" (semicolon). There are also no quotes.

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

A friend mentioned that I needed to do something with the encoding, but we did not have time to discuss this.

How can I iterate over these (curious) Alert objects that I created and turn them into JSON objects in my Java so that I can access their properties like alert.date and alert.message .

I thought the screenshots would help you understand what I am doing. Firebase is not protected at all, so you can freely look at it. It will not be very much, and when I go to production, I will transfer it anyway.

Firebase Screenshot

JSON export directly from Firebase site

Code in Android Studio for printing each object in the console

Print to console when application starts

I'm sure this is a very simple question to answer, I'm just not too good at JSON and coding in general.

Thanks!

+6
source share
4 answers

You cannot access JSON natively in Java.

But the Firebase DataSnapshot class provides everything you need.

If you have a DataSnapshot data on fbAlerts in the 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 create a Java class that represents a warning. See the Firebase Android Reading Guide . Examples.

+7
source

you can use 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); } 
+3
source

Use this method to convert jsonObject form to dataSnapshot

 Map<String, String> value = (Map<String, String>) dataSnapshot.getValue(); Log.i("dataSnapshot", "dataSnapshot" + new JSONObject(value)); 
+2
source

From the above code, you will see that you have a JSONArray, not a JSONObject.

In this case, you need to do something like the following:

 // Find the right array object JSONArray jsonArray = response.getJSONArray("fbAlerts"); // Loop through the array for (int i=0; i< jsonArray.length(); i++) { JSONObject myObj = jsonArray.getJSONObject(i); strMsg = myObj.getString("message"); } 

In the example, when you repeat groups, this apparently points to an array and therefore needs an iterator to access the contents of the object.

0
source

All Articles