-1

So here is the JSON

{
  "response": {
    "data": {
      "images": {
        "image": [
      {...

I want to access the image array. I was wondering if the following code was the best way to do it:

JSONObject jsonObjectResponse = new JSONObject(jsonString);
JSONObject jsonResponse = jsonObjectResponse.getJSONObject("response");

JSONObject jsonObjectData = new JSONObject(jsonResponse.toString());
JSONObject jsonData = jsonObjectData.getJSONObject("data");

JSONObject jsonObjectImages = new JSONObject(jsonData.toString());
JSONObject jsonImages = jsonObjectImages.getJSONObject("images");

JSONObject jsonObjectImage = new JSONObject(jsonImages.toString());
JSONArray jsonImageArray = jsonObjectImage.getJSONArray("image");

I feel like this requires a lot of time for a simple task. Does anyone have a more "engineered" solution? Thanks a lot!

Adem
  • 35
  • 5

7 Answers7

1

No, that's not the best way to do it. You keep converting from JSONObjects to Strings and then back to JSONObjects again. You should be able do:

JSONArray jsonImageArray = new JSONObject(jsonString)
    .getJSONObject("response")
    .getJSONObject("data")
    .getJSONObject("images")
    .getJSONArray("image");

Even if you just remove all of the toString calls, it becomes a lot more concise (but I would still favour the above example):

JSONObject jsonObjectResponse = new JSONObject(jsonString);
JSONObject jsonResponse = jsonObjectResponse.getJSONObject("response");
JSONObject jsonData = jsonResponse.getJSONObject("data");
JSONObject jsonImages = jsonData.getJSONObject("images");
JSONArray jsonImageArray = jsonImages.getJSONArray("image");
Michael
  • 41,989
  • 11
  • 82
  • 128
1

I think you can simply combine all these statements in the following way:

JSONObject jsonObjectResponse = new JSONObject(jsonString);
JSONArray jsonImageArray = jsonObjectResponse.getJsonObject("data")
                              .getJsonObject("images")
                              .getJsonArray("image");
mave_rick
  • 59
  • 8
0

Can you not chain the getJSONObject function?

JSONObject jsonObjectResponse = new JSONObject("jsonString");
JSONObject jsonImages = jsonObjectResponse.getJSONObject("response").getJSONObject("data")....
speedster01
  • 433
  • 3
  • 19
0

Try following:

JSONObject jsonObjectResponse = new JSONObject(jsonString);

JSONArray imageArray = jsonObjectResponse.getJSONObject("response")
     .getJSONObject("data")
     .getJSONObject("images")
     .getJSONArray("image")

Hope it would work.

Ankit Mehta
  • 4,251
  • 4
  • 19
  • 27
0

You can user chain calls:

String jsonString = "{\"response\":{\"data\":{\"images\":{\"image\":[]}}}}";

JSONObject json = new JSONObject(jsonString);

JSONArray image = json.getJSONObject("response") 
                      .getJSONObject("data")
                      .getJSONObject("images")
                      .getJSONArray("image");

E.g. your image objects like {"title":"Some image","url":"http://..."}, so:

JSONObject firstImage = image.getJSONObject(0);
String url = firstImage.getString("url");

Or you can use GSON. Your classes:

class ServerAnswer {
    private Response response;

    // constructors, getters, setters
}

class Response {
    private Data data;

    // constructors, getters, setters
}

class Images {
    @SerializedName("image")
    private Image[] images;

    // constructors, getters, setters
}

class Image {
    private String url, title;

    // constructors, getters, setters
}

And parse:

ServerAnswer answer = new Gson().fromJson(jsonString, ServerAnswer.class);

Image[] images = answer.getResponse().getData().getImages().getImagesArray();
Image firstImage = images[0];
String url = firstImage.getUrl();
Peter Samokhin
  • 864
  • 1
  • 9
  • 19
0

In your case:

   public class Images {
        @SerializedName("image")
        public ArrayList<String> image;
   }

   public class Data {
        @SerializedName("images")
        public Images images;
   }

   public class Response {
        @SerializedName("data")
        public Data data;
   }

   public class MyData {
        @SerializedName("response")
        public Response response;
   }

then

MyData data = new Gson().fromJson(yourJsonString, MyData.class);

I think this is the best approach

Liar
  • 1,235
  • 1
  • 9
  • 19
0

You can try following code.

JSONArray jsonResponseArray = new JSONObject(strJson).getJSONObject("response").getJSONObject("data").getJSONObject("images").getJSONArray("image");

Hope this will help you.