2

I have a question about getting jsonobjects out of a jsonobject.

This is what im getting back:

{
"data": {
    "someid": {
        "air_by_date": 0, 
        "cache": {
            "banner": 1, 
            "poster": 1
        }, 
        "language": "en", 
        "network": "somenetwork", 
        "next_ep_airdate": "somedate", 
        "paused": 0, 
        "quality": "somequality", 
        "show_name": "somename", 
        "status": "Continuing", 
        "tvdbid": someid, 
        "tvrage_id": someid, 
        "tvrage_name": "Showname"
    }, 
    "someid": {
        "air_by_date": 0, 
        "cache": {
            "banner": 1, 
            "poster": 1
        }, 
        "language": "en", 
        "network": "somenetwork", 
        "next_ep_airdate": "", 
        "paused": 0, 
        "quality": "somequality", 
        "show_name": "somename", 
        "status": "Continuing", 
        "tvdbid": someid, 
        "tvrage_id": someid, 
        "tvrage_name": "somename"
    }, 

But how am i supposed to create "ShowObjects" of them. I know how it works with JSONArrays but i have never done this kind of JSON before.

This is what i got untill now:

String json = download(url);

JSONObject result = new JSONObject(json);
JSONObject resultData = result.getJSONObject("data");
Timmeeh93
  • 223
  • 1
  • 4
  • 14
  • Are you wanting to loop through those numeric entries inside `data` and turn each of those into a `JSONObject`? If Yes, then [this](http://stackoverflow.com/a/13573965/802469) will help. – Reed Oct 17 '14 at 18:56
  • What you have should work just fine. What happens / does not work? – Henry Oct 17 '14 at 18:57
  • @jakar Those numeric objects are representing a show, but how do i loop through it? Because its not an JSONArray i get returned, im a little confused on how to do it. – Timmeeh93 Oct 17 '14 at 19:00
  • @Timmeeh93: That is not JSON string you got over there. – Voicu Oct 17 '14 at 19:14
  • do you want to create your own array of objects or fetch a specific object (someid) – Roman Rozenshtein Oct 17 '14 at 20:54
  • that is not a valid json object. the same key is present twice. – njzk2 Oct 17 '14 at 21:09
  • (if you want a jsonarray, take a look at `JSONObject.names()`) – njzk2 Oct 17 '14 at 21:10

3 Answers3

0

Copied from this answer:

Use the keys() iterator to iterate over all the properties, and call get() for each.

Iterator<String> iter = json.keys();
while (iter.hasNext()) {
    String key = iter.next();
    try {
        Object value = json.get(key);
    } catch (JSONException e) {
        // Something went wrong!
    }
}
Community
  • 1
  • 1
Reed
  • 14,703
  • 8
  • 66
  • 110
  • I'm still dont get it on how to create my own show object out of this. – Timmeeh93 Oct 17 '14 at 19:38
  • If speed is a priority, then I would recommend just manually setting values of your `Show` object. ex: `show.setLanguage(showJson.getString('language'))`; Alternatively, you can use reflection to set property values based on the keys from the JSON where the JSON key = property name. This will negatively impact performance, though. If you want, i can update with a basic example for reflection – Reed Oct 17 '14 at 21:23
  • Or you can use a third-party json library that already handles that sort of thing. See the answer by Drejc – Reed Oct 17 '14 at 21:25
0

Try using Jackson or Gson for these tasks.

With Jackson you would do something like this:

class Response() {
     public String message;
     public String result;
     public Map<String, Object> data; 
}

ObjectMapper mapper = new ObjectMapper()
Resonse respone = mapper.readValue(json, Response.class)

or use a custom deserializer to read those values out.

Drejc
  • 14,196
  • 16
  • 71
  • 106
0

I will give you an example how to fetch the objects and vars in your "data" json:

lets say the second "someid" is : "123". We will fetch the second jsonObject now :

JSONObject secondJsonSomeId = resultData.getJsonObject("123"); 

Now we will fetch the "banner" ( int =1 ) of the jsonObject named "cache" located in our secondJsonSomeId :

int bannerInsecondJsonSomeId = secondJsonSomeId.getJsonObject("cache").getInt("banner");// bannerInsecondJsonSomeId == 1

the code is not compiled so there might be errors , ask if you have additional questions.