Skip to content Skip to sidebar Skip to footer

Gson Make Firebase-like Json From Objects

In the past a manually created as list of data, which was stored locally in a database. Now I would like to parse those data and put them through import json option into firebase d

Solution 1:

Your "temp fix" will store each object as a string not a object.

You need to serialize an object to get that data, not a list.

For example, using a Map

List<Card> cards = myDatabase.retrieveData();
Map<Map<String, Object>> fireMap = newTreeMap<>();
int i = 1;
for (Card c : cards) {
    Map<String, Object> cardMap = newTreeMap<>();
    cardMap.put("text", c.getText());
    fireMap.put("id_" + (i++), cardMap);
}
Gson gson = newGson();
String data = gson.toJson(fireMap);

Post a Comment for "Gson Make Firebase-like Json From Objects"