I am new to firebase, and I am trying to put json object data into my firebase. I know how to put data as a class object into firebase, But I want to put json object data.
Yes, you can achieve this. Start by converting your json to a string then do the following:
String jsonString; //set to json string
Map<String, Object> jsonMap = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, Object>>() {}.getType());
I am using the "updateChildren" method because I want the JSON object added directly to the root of my child object
firebaseDatabaseRef.child("users").child(uid).updateChildren(jsonMap);
If you don't care or you would like to set a new node, use
firebaseDatabaseRef.child("users").child(uid).child({your new node}).setValue(jsonMap);
As suggested here: Convert a JSON String to a HashMap
you can easily convert your json object/string to a Map<String, Object>
(you don't even need to create a POJO):
Firebase ref = new Firebase(YOUR_FIREBASE_REF);
Map<String, Object> map = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, Object>>() {}.getType());
ref.setValue(map);
It is so simple to also save JSON value on firebase. Let's say you have called your firebase reference
Firebase ref = new Firebase("https://docs-examples.firebaseio.com/android/saving-data/fireblog");
Then you will pass your data with Map
objects. Map
keeps values with keys. In your case you will have a String
key and JSONObject
value.
Firebase userRef = ref.child("user");
Map<String, JSONObject> userMap= new HashMap<String, JSONObject>();
JSONObject tempObject = new JSONObject();
try{
tempObject.put("birthday","1992");
tempObject.put("fullName","Emin AYAR");
}catch(Exception e){
e.printStackTrace();
}
userMap.put("myUser", tempObject);
userRef .setValue(userMap);
Solution In Kotlin because Auto Conversion convert it with compile time error saying : "Not enough information to infer parameter T" with Kotlin and Android
val jsonMap = Gson().fromJson<Any>(stringJson, object : TypeToken<HashMap<String, Any>>() {}.type)
As you've tagged android in your question, so I guess you want to put some data in your firebase database from the Android client.
You've some JSON data and you want it to store in firebase database right? This is the step by step procedure to get it done.
I don't really know why are you trying to avoid POJO. It makes the total operation more simplified. Now let us assume you've a JSON string. You need to map your JSON to a java class to put it directly in the firebase. Firebase stores the data in a JSON-like format which is easily understandable.
I use Gson to map a JSON string to a specific java object. Its the easiest method I've found so far needs few lines of coding.
Gson gson = new Gson();
YourPOJO myPOJO = gson.fromJson(jsonString, YourPOJO.class);
Now you've mapped your JSON string to myPOJO
object successfully. You're ready to put this object in your Firebase database now.
Lets take the reference to your Firebase database and then call setValue
in that specific node where you want put the data.
Firebase ref = new Firebase(YOUR_FIREBASE_REF);
ref.setValue(myPOJO);
Simple!