如何在Android中使用REST API从Firebase数据库检索数据? [英] How to retrieve the data from Firebase database using REST APIs in android?

查看:122
本文介绍了如何在Android中使用REST API从Firebase数据库检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用fire base时,当尝试使用fire base rest api检索数据时,我会得到这样的响应,

First time i am working with fire base,when am trying to retrieve the data using fire base rest apis i get the response like this,

{
  "-JQjT7REctmFfOAoGI6d" : {
    "age" : "23",
    "name" : "xxx",
    "id" : "1"
  },
  "-JQjT7RGTUdl1FTrjed6" : {
     "age" : "34",
    "name" : "xxx",
    "id" : "1"
  }
}

它包含多个具有不同ID且没有jsonarray的json对象.我想将此数据存储在对象中.

it contain multiple json objects with diffrent ids without jsonarray.i want to store this data in objects.

推荐答案

如果仅由于键而在解析时遇到问题,则可以使用迭代器提取键并对其进行解析

If you are having problem in parsing just because of keys then you can use a iterator and extract the keys and parse them

if(jObj != null){
    Iterator<Object> keys = jObj.keys();
    while(keys.hasNext()){
        String key = String.valueOf(keys.next()); // this will be your JsonObject key
        JSONObject childObj = jObj.getJSONObject(key);
        if(childObj != null){
             //Parse the data inside your JSONObject
        }
    }
}

这篇关于如何在Android中使用REST API从Firebase数据库检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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