You need a class structure like this (pseudo-code):
class Response
String id
DataList posts
class DataList
List<Data> data
class Data
String message
From from
String id
String created_time
DataList comments
class From
String name
String id
Note that you can change class names if you want, but you have to keep the attribute names to match the field names in the JSON response.
Also note that I’ve used the same class Data
to store the data for the posts and for the comments. This is possible because the data for comments is a subset of the data for posts, so when parsing a comment, Gson will just ignore the attributes created_time
and comments
…
Then parse the JSON with:
Gson gson = new Gson();
Response response = gson.fromJson(yourJsonString, Response.class);
Now you can get the i post with:
String postMessage = response.getPosts().getData().get(i).getMessage();
And in the same way you can get its comments with:
String commentMessage = response.getPosts().getData().get(i).getComments().getData().get(i).getMessage();
NOTE: Using an Online JSON Viewer will help you to clearly see the class structure you need to wrap your JSON data!
0
solved complex json to java object conversion using gson