1

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"
  }
}

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

4
  • Did you try sample code given on Firebase site for data retrieving ? Commented Jul 30, 2016 at 6:28
  • i dont want to using firebase lib,i want firebase rest apis only Commented Jul 30, 2016 at 6:38
  • @Rgv where are you facing problem? are you facing any parsing the above JSON?.. Also can you tell if you want to use the Listener (like addValueEventListener) or not? Commented Jul 30, 2016 at 6:44
  • @ Amit Upadhyay,am using rest apis for firebase not using lib Commented Jul 30, 2016 at 6:53

2 Answers 2

3

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
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank your for your replay,i will try this
,its workingThank you,Vishal Patoliya said directly rest api is not suppoted by firebase.your sloution working for me, whats the right way to do this
If you are using Firebase then it's advisable to use according to their doc. So better add their library and parse according to their doc.
2
you can access data by mainly 3 ways
1 addValueEventListener
2 addChildEventListener
3 addListenerForSingleValueEvent

Step 1: Create Model Class

public class UserModel {

    String id;
    String name;
    String age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

Step 2: Access Data In Activity

 Firebase firebaseUserDataReference = new Firebase("your firebase url").child("your root name/");

firebaseUserDataReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                Log.d("Call", String.valueOf(dataSnapshot.getValue()));
                for (DataSnapshot postData : dataSnapshot.getChildren()) {
                    UserModel userModel= postData.getValue(UserModel.class);
                    yourArrayList.add(userModel);
                }             
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

5 Comments

thank for your replay,sorry i not want to using firebase lib,i want firebase rest apis only
rest api is for web !! if you want to use firebase in android you need to add firebase lib in gradle dependency!!
are you sure,is there any other way for android rest apis
yes if you want to use firebase you must use firebase lib in android
directly rest api is not suppoted by firebase so , firebase give us to there default functions which is in library so by using that you can use firebase database!! you don't require to use rest api manually

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.