Parsing nested JSON data using GSON

You just need to create a Java class structure that represents the data in your JSON. In order to do that, I suggest you to copy your JSON into this online JSON Viewer and you’ll see the structure of your JSON much clearer…

Basically you need these classes (pseudo-code):

class Response
  Data data

class Data
  List<ID> id

class ID
  Stuff stuff
  List<List<Integer>> values
  String otherStuff

Note that attribute names in your classes must match the names of your JSON fields! You may add more attributes and classes according to your actual JSON structure… Also note that you need getters and setters for all your attributes!

Finally, you just need to parse the JSON into your Java class structure with:

Gson gson = new Gson();
Response response = gson.fromJson(yourJsonString, Response.class);

And that’s it! Now you can access all your data within the response object using the getters and setters…

For example, in order to access the first value 456, you’ll need to do:

int value = response.getData().getId().get(0).getValues().get(0).get(1);

Leave a Comment