在android中将json对象保存到firebase中 [英] saving json object to firebase in android

查看:159
本文介绍了在android中将json对象保存到firebase中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是firebase的新手,我试图将json对象数据放入我的firebase。
我知道如何将数据作为一个类对象放入firebase,但我想把json对象的数据。

有什么办法可以把json对象的数据到firebase没有转换为pojo?

解决方案

当你在你的问题上标记了android,所以我想你想将一些数据从Android客户端放入您的Firebase数据库中。

你有一些JSON数据,你想把它存储在firebase数据库中吗?这是一步一步的过程来完成它。



我不知道你为什么要避免POJO。它使整个操作更简化。现在让我们假设你有一个JSON字符串。你需要将你的JSON映射到一个java类,直接放在firebase中。 Firebase以类似JSON的格式存储数据,这很容易理解。

我使用Gson将JSON字符串映射到特定的java对象。迄今为止,我发现的最简单的方法只需要几行代码。

  Gson gson = new Gson(); 
YourPOJO myPOJO = gson.fromJson(jsonString,YourPOJO.class);

现在您已将您的JSON字符串映射到 myPOJO 对象成功。您已准备好将此对象放入您的Firebase数据库中。

让我们引用您的Firebase数据库,然后在您要放置数据的特定节点中调用 setValue

  Firebase ref =新的Firebase(YOUR_FIREBASE_REF); 
ref.setValue(myPOJO);

简单!

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.

Is there any way to put json object data into firebase without converting it as a pojo?

解决方案

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!

这篇关于在android中将json对象保存到firebase中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆