Java - Gson parsing nested within nested
I have to interact with an API, and the response format (from what I've read) seems to be poorly structured. I've found a google groups reply to a somewhat similiar problem here, but I'm having trouble implementing a Response class to handle the Gson.fromJson. Is there an example I'm missing that's out there?
{
"response":{
"reference": 1023,
"data":{
"user":{
"id":"210",
"firstName":"john",
"lastName":"smith",
"email":"[email protected]",
"phone":"",
"linkedid":{
"id":"238"
}
}
}
}
}
The JSON objects {}
can be represented by a Map<String, Object>
or a Javabean class. Here's an example which uses a Javabean.
public class ResponseData {
private Response response;
// +getter+setter
public static class Response {
private int reference;
private Data data;
// +getters+setters
}
public static class Data {
private User user;
// +getter+setter
}
public static class User {
private String id;
private String firstName;
private String lastName;
private String email;
private String phone;
private Linkedid linkedid;
// +getters+setters
}
public static class Linkedid {
private String id;
// +getter+setter
}
}
Use it as follows:
ResponseData responseData = new Gson().fromJson(json, ResponseData.class);