android - 在 Firebase 数据库中添加数据

标签 android json firebase firebase-realtime-database

我正在迁移到 Firebase 以帮助我检索数据。我的问题是如何像下面的 JSON 一样格式化我的 Firebase 数据库数据。我如何指定像下面的 JSON 这样的对象?

[
  {
    "DoctorsName": "DR.LUTAAYA HUZAIFAH IDRIS",
    "HealthCentreName": "Mulago",
    "Specialisation": "Dentist",
    "Information": "am genious",
    "photo": "huzaifah.jpg",
    "WorkingHours": "8:00am - 3:00pm",
    "PhoneNumber": "0704594180",
    "Email": "huxaiphaeridris@gmail.com"
  },
  {
    "DoctorsName": "DR. G. WABULEMBO",
    "HealthCentreName": "Kibuli",
    "Specialisation": "Opthalmologist",
    "Information": " is a consultant Opthalmologist with over 25 years experience in Canada and Uganda.",
    "photo": "wabulembo.jpg",
    "WorkingHours": "8:00am - 12:00pm",
    "PhoneNumber": "0756478923",
    "Email": "wabulembo@gmail.com"
  },
  {
    "DoctorsName": "DR. BUKIRWA JAUHARAH",
    "HealthCentreName": "Kibuli",
    "Specialisation": "Urologist",
    "Information": "Shes a Urologist , she has an experience of 7 years in Kibuli Hospital , and she studied in the United States Of America University",
    "photo": "jauharah.jpg",
    "WorkingHours": "10:00am - 4:00pm",
    "PhoneNumber": "0706081457",
    "Email": "bukijauha@gmail.com"
  },
  {
    "DoctorsName": "DR. R. SEKITOLEKO",
    "HealthCentreName": "Nakasero Hospital",
    "Specialisation": "General Physician",
    "Information": " is consultant physician with over 33 years of experience in Berlin and Hamburg (Germany) and Mulago Hospital (Uganda). He also practised as a consultant with WHO (2001-2003).",
    "photo": "sekitoleko.jpg",
    "WorkingHours": "12:00am - 7:00pm",
    "PhoneNumber": "0765633667",
    "Email": "sekitoleko@gmail.com"
  },
  {
    "DoctorsName": "DR. A. MAKHOBA ",
    "HealthCentreName": "Nakasero Hospital",
    "Specialisation": "Cardiologist",
    "Information": "is a consultant cardiologist,. He has practised for more than 20 years in Illinois and Wisconsin (USA) and for over two years in Uganda, at the Uganda Heart Institute and Nakasero Hospital.",
    "photo": "makhoba.jpg",
    "WorkingHours": "8:00am- 4:00pm",
    "PhoneNumber": "0765453636",
    "Email": "makhoba@gmail.com"
  },
  {
    "DoctorsName": "DR.Sako Banabus",
    "HealthCentreName": "Nakasero Hospital",
    "Specialisation": "Dentist",
    "Information": null,
    "photo": "kamanzi.jpg",
    "WorkingHours": "7:00pm - 3:00am",
    "PhoneNumber": "0754369696",
    "Email": "sw@gmail.com"
  },
  {
    "DoctorsName": "DR. KAMANZI ABUBAKAR",
    "HealthCentreName": "Mengo Hosiptal",
    "Specialisation": "Neurosurgeon",
    "Information": "Hes a Neurosurgeon, he has an experience of 20 years in Mengo Hospital , he studied in the Carlifornia University",
    "photo": "kamanzi.jpg",
    "WorkingHours": "7:00pm - 3:00am",
    "PhoneNumber": "07003014850",
    "Email": "kamanziabubakar75@gmail.com"
  }
]

最佳答案

您几乎可以将任何您喜欢的 JSON 直接写入 Firebase。不过,为了便于检索,您需要找出某种模式。我推荐如下内容:

doctors
  <docotrId>
    - doctorId
    - doctorName
    - specialisation
    - information
    - photo
    - phone
    - email
    - @healthCenter
healthCenters
  <healthCenterId>
    - centerId
    - name
    - @doctors[]
specialisationDoctors
  <specialisationName>
    <doctorId>

您需要做的第一件事是将您的数据映射到此模式。您可以创建 POJO 类、映射,甚至 JSON 格式的字符串。

然后在适当的节点上使用 setValue() 推送映射数据。请务必阅读 documentation获取更多信息。

您可能还想查看我正在开发的名为 Firebomb 的 Firebase 对象映射器(不要脸的插件)。 Android 版本即将推出。

最后使用适当的 Firebase event listeners 检索数据.

关于android - 在 Firebase 数据库中添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39551193/

相关文章:

java - 在 android 服务中使用前置摄像头拍照

json - 播种 Node/MongoDB 应用程序的最佳方法是什么?

json - 如何用 Go 结构体来表示这个复杂的数据结构?

ios - Firebase 身份验证撤销 token 在 SwiftUI 应用程序中不起作用

firebase - Stripe firebase扩展,监听收集组中的发票收集

firebase - Flutter/Firebase_Auth:生成函数返回null

java - 根据选择的微调器值生成纯文本字段?

java - Android, Mediaplayer mp3, 令人不安的声音

Android:性能问题多个纹理 Opengl ES

json - 将 R 表转换为 JSON 字符串