在Firebase数据库中添加数据

时间:2016-09-17 20:04:53

标签: android json firebase firebase-realtime-database

我正在迁移到Firebase以帮助我检索我的数据。我的问题是如何格式化我的Firebase数据库数据,如下面的JSON。如何指定下面的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"
  }
]

1 个答案:

答案 0 :(得分:1)

你几乎可以直接写入任何你想要使用Firebase的JSON。为了便于检索,您需要找出某种模式。我推荐以下内容:

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检索数据。