retrieve firebase data as json

18,108

Solution 1

obviously the pojo class is the most neat and the easiest way to achieve my goal

first

created the classes that represents my json file:

for that json file:

{ "data" : { "text" : { "loading" : { "intro_text" : "hi" } } } }

create these classes:

pojo, data, text, loading such like that:

public class Pojo{
    private Data data;

    public void setData(Data data){
        this.data = data;
    }
    public Data getData(){
        return this.data;
    }

public class Data {
    private Text text;

    public void setText(Text text){
        this.text = text;
    }
    public Text getText(){
        return this.text;
    }
}

public class Text {
    private Loading loading;

    public void setLoading(Loading loading){
        this.loading = loading;
    }
    public Loading getLoading(){
        return this.loading;
    }
}

public class Loading {
    private String intro_text;

    public void setIntro_text(String intro_text){
        this.intro_text = intro_text;
    }
    public String getIntro_text(){
        return this.intro_text;
}

}

Second

apply this method on this part of file assuming that (data) is the first sub node in the json file, so the is ref = "" (no ref as it is the first node)

 Database database = FirebaseDatabase.getInstance();
 DatabaseReference databaseReference = database.getReference("");
 databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Pojo pojo = dataSnapshot.getValue(Pojo.class);
        Gson gson = new Gson();
        String reqJson= gson.toJson(pojo);
        return reqJson
    }

Solution 2

Here is what I do. First, create a java object from DataSnapshot then get JSON from that object using Gson.

Object object = datasnapshot.getValue(Object.class);
String json = new Gson().toJson(object);

Solution 3

You'll get the returned value that you're getting when you try to convert a json to String.

So, instead of doing that, you can use one of the following methods :-

First :-

String lastUpdate = dataSnapshot.child(last_update).getValue(String.class);
String last_update_code = dataSnapshot.child(last_update_code).getValue(String.class);

Second :-

Define a POJO that maps to your Json. After that, use the following code.

YourPOJO yourPOJO = dataSnapshot.getValue(YourPOJO.class);

You can read more about it here.

Solution 4

One approach is to use Gson to convert result to json string e.g.

String jsonString = gson.toJson(dataSnapshot.getValue());
Share:
18,108
Aya
Author by

Aya

Updated on June 16, 2022

Comments

  • Aya
    Aya almost 2 years

    I would like to retrieve data stored in firebase database but in proper Json format, the problem is when using String.valueOf(dataSnapshot.getValue()) it is not the correct json format. For example, if the the data

    {
      "properties" : {
         "last_update" : "15-02-2017 02:48:00 pm",
         "last_update_code" : "1258884"  
      } 
    }
    

    the returned value becomes like that

    properties = {last_update = 15-02-2017 02:48:00 pm,last_update_code = 1258884}
    

    without the quotes or (:) .

    so it there a way to get the data as string in the first format?