Android Firebase - 无法从Firebase快照接收正确的JSON [英] Android Firebase - Can't receive proper JSON from Firebase snapshot

查看:135
本文介绍了Android Firebase - 无法从Firebase快照接收正确的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用程序连接到Firebase并提取由我的服务器发送的警报对象。

My android app connects to Firebase and pulls "Alert Objects" that are sent there by my server.

当我从Firebase导出数据时,我得到了一个漂亮的格式化数据的JSON表示。

When I export the data from Firebase, I get a beautifully formated JSON representation of the data.

问题:
当我使用DataSnapshot将数据拉到我的Android设备时,数据有'='(等号)而不是':'(分号)。引号也不存在。

Problem: When I pull the data to my android device using a DataSnapshot, the data has '=' (equals signs) instead of ':' (semicolons). Also the quotations are not there.

当我尝试做类似的事情时JSONObject alert = new JSONObject(data.getValue()。toString() ); 由于显而易见的原因我收到错误。我说得很明显,因为如果你看看我的代码打印到控制台的内容,就可以看到它不再是有效的JSON格式。

When I try to do something like JSONObject alert = new JSONObject(data.getValue().toString()); I get errors for obvious reasons. I say obvious because if you look at what my code prints to the console you can see that it is no longer in valid JSON format.

一位朋友提到我需要做一些编码,但我们没有时间讨论它。

A friend mentioned that I need to do something with encoding but we didn't have time to discuss it.

如何迭代我创建的这些(有点奇怪的)警报对象并将它们转换为Java中的JSON对象,以便我可以访问其属性,如 alert.date alert.message

How can I iterate through these (kinda weird) Alert Objects that I have created and turn them into JSON objects within my Java so that I can access their properties like alert.date and alert.message.

我认为截图会帮助你看到我在做什么。 Firebase完全不受保护,因此您可以随意查看它。它不会真正做太多,当我开始制作时,无论如何我都会移动它。

I thought screenshots would help you see what I am doing. The firebase is not secured at all so you can feel free to take a look at it. It won't really do much and when I go to production I will be moving it anyways.

我确定这是一个非常容易回答的问题,我只是不太熟悉JSON和编码整体。

I am sure this is a super easy question to answer, I am just not too well versed with JSON and encoding as a whole.

谢谢!

推荐答案

您无法在Java中本地访问JSON。

You cannot access JSON natively in Java.

Firebase DataSnapshot class 提供您需要的一切。

But the Firebase DataSnapshot class provides everything you need.

如果您的屏幕截图中的 fbAlerts 的数据有 DataSnapshot ,您可以打印日期+消息和收件人每个:

If you have a DataSnapshot of the data at the fbAlerts in your screenshot, you can print the date+message and recipients for each:

for (DataSnapshot alert: alerts.getChildren()) {
  System.out.println(alert.child("date").getValue();
  System.out.println(alert.child("message").getValue();
  for (DataSnapshot recipient: alert.child("recipients").getChildren()) {
    System.out.println(recipient.child("name").getValue();
  }
}

或者,您可以构建一个代表警报的Java类。请参阅关于在Android中阅读数据的Firebase指南,以此为例。

Alternatively, you can build a Java class that represents an alert. See the Firebase guide on reading data in Android for examples of that.

这篇关于Android Firebase - 无法从Firebase快照接收正确的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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