saving json object to firebase in android

前端 未结 5 1690
独厮守ぢ
独厮守ぢ 2021-01-02 08:07

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.

5条回答
  •  情歌与酒
    2021-01-02 08:45

    Yes, you can achieve this. Start by converting your json to a string then do the following:

      String jsonString; //set to json string
      Map jsonMap = new Gson().fromJson(jsonString, new TypeToken>() {}.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);
    

提交回复
热议问题