Fetch an item in the http json Object

Asked

Viewed 156 times

0

Well I am "beginner" on android. And in my project, I’m getting from http reponse (with the GET method) a string for Jsonobject.

"{ "status":{ "d3": { "stats" : false },    "a1": { "stats" : false } } }"

But I can’t do the function that takes the D3/a1.

1 answer

2


All you need to do is fetch the Jsonobject and then fetch the first element as:

JSONObject obj = new JSONObject (stringJson);
JSONObject status = obj.getJSONObject("status");
JSONObject d3 = status.getJSONObject("d3");

and so you will always be able to catch your values by following this concept.

But I suggest you do a little research on how to convert Json for object that will greatly facilitate your life.

  • The last line is not supposed to be JSONObject d3 = status.getJSONObject("d3"); ?

  • I put it like that and it worked so maybe it’s supposed to

  • I’m just commenting from a perspective of getting the answer to be as correct as possible. So it was certainly a constructive comment. If the status is inside obj if made obj.getJSONObject( to get it, then to get the d3 inside status it would make sense to be status.getJSONObject(. Or I’m the one thinking wrong?

  • @Isac, yes you are correct I ended up putting the incorrect name Thank you for the touch already corrected now is correct! :)

  • @Ezequielmessore No problem. Now it got better. I gave it my +1 too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.