Parsing a list of JSON objects using GSON

I have a JSON object, for example:

{ "user1": { "timeSpent": "20.533333333333335h", "worklog": [ { "date": "06/26/2013", "issues": [ { "issueCode": "COC-2", "comment": "\ncccccc", "timeSpent": "20.533333333333335h" } ], "dayTotal": "20.533333333333335h" } ] }, "admin": { "timeSpent": "601.1h", "worklog": [ { "date": "06/25/2013", "issues": [ { "issueCode": "COC-1", "comment": "", "timeSpent": "113.1h" } ], "dayTotal": "113.1h" }, { "date": "06/26/2013", "issues": [ { "issueCode": "COC-1", "comment": "", "timeSpent": "8h" }, { "issueCode": "COC-2", "comment": "", "timeSpent": "480h" } ], "dayTotal": "488h" } ] } } 

and try to parse it using Gson:

 Gson gson = new Gson(); Book responseBean = gson.fromJson(jsonString, Book.class); 

But "responceBean" is always "null"

Here are all the other classes:

 public class Book { private List<User> user = new LinkedList<User>(); public List<User> getUser() { return user; } public void setUser(List<User> user) { this.user = user; } } public class User { private String timeSpent; private List<WorkLog> worklogs = new LinkedList<WorkLog>();; public List<WorkLog> getWorklogs() { return worklogs; } public void setWorklogs(List<WorkLog> worklogs) { this.worklogs = worklogs; } public String getTimeSpent() { return timeSpent; } public void setTimeSpent(String timeSpent) { this.timeSpent = timeSpent; } } public class WorkLog{ private String date; private String dayTotal; private List<Issues> issues; public String getDate(){ return this.date; } public void setDate(String date){ this.date = date; } public String getDayTotal(){ return this.dayTotal; } public void setDayTotal(String dayTotal){ this.dayTotal = dayTotal; } public List<Issues> getIssues(){ return this.issues; } public void setIssues(List<Issues> issues){ this.issues = issues; } } public class Issues{ private String comment; private String issueCode; private String timeSpent; public String getComment(){ return this.comment; } public void setComment(String comment){ this.comment = comment; } public String getIssueCode(){ return this.issueCode; } public void setIssueCode(String issueCode){ this.issueCode = issueCode; } public String getTimeSpent(){ return this.timeSpent; } public void setTimeSpent(String timeSpent){ this.timeSpent = timeSpent; } } 

This is my last attempt. For some reason, I can’t understand the right way. We will be very grateful for any help.

+4
source share
2 answers

Your JSON model does not match your object model .

An intermediate level is required to fill the gap: TypeAdapter .

In addition, there is no name information for the user.

And finally, there is a name mismatch: "worklog" in JSON, "worklogs" in Java.

Here is the fixed version:

Java Model:

 class User { private String timeSpent; @SerializedName("worklog") private List<WorkLog> worklogs = new LinkedList<WorkLog>(); private String name; public List<WorkLog> getWorklogs() { return worklogs; } public void setWorklog(List<WorkLog> worklogs) { this.worklogs = worklogs; } public String getTimeSpent() { return timeSpent; } public void setTimeSpent(String timeSpent) { this.timeSpent = timeSpent; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 

Plumbing to fill in the gap:

 class BookTypeAdapter implements JsonSerializer<Book>, JsonDeserializer<Book> { Gson gson = new Gson(); public JsonElement serialize(Book book, Type typeOfT, JsonSerializationContext context) { JsonObject json = new JsonObject(); for (User user : book.getUser()) { json.addProperty(user.getName(), gson.toJson(user)); } return json; } public Book deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject json = element.getAsJsonObject(); Book book = new Book(); for (Entry<String, JsonElement> entry : json.entrySet()) { String name = entry.getKey(); User user = gson.fromJson(entry.getValue(), User.class); user.setName(name); book.getUser().add(user); } return book; } } 

And the circuit:

 GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Book.class, new BookTypeAdapter()); Gson gson = builder.create(); Book book = gson.fromJson("{" + " \"user1\": {" + " \"timeSpent\": \"20.533333333333335h\"," + " \"worklog\": [" + " {" + " \"date\": \"06/26/2013\"," + " \"issues\": [" + " {" + " \"issueCode\": \"COC-2\"," + " \"comment\": \"\ncccccc\"," + " \"timeSpent\": \"20.533333333333335h\"" + " }" + " ]," + " \"dayTotal\": \"20.533333333333335h\"" + " }" + " ]" + " }," + " \"admin\": {" + " \"timeSpent\": \"601.1h\"," + " \"worklog\": [" + " {" + " \"date\": \"06/25/2013\"," + " \"issues\": [" + " {" + " \"issueCode\": \"COC-1\"," + " \"comment\": \"\"," + " \"timeSpent\": \"113.1h\"" + " }" + " ]," + " \"dayTotal\": \"113.1h\"" + " }," + " {" + " \"date\": \"06/26/2013\"," + " \"issues\": [" + " {" + " \"issueCode\": \"COC-1\"," + " \"comment\": \"\"," + " \"timeSpent\": \"8h\"" + " }," + " {" + " \"issueCode\": \"COC-2\"," + " \"comment\": \"\"," + " \"timeSpent\": \"480h\"" + " }" + " ]," + " \"dayTotal\": \"488h\"" + " }" + " ]" + " }" + "}", Book.class); String json = gson.toJson(book); 

Take a look at my tutorial to get an idea of ​​what is possible with Gson: Mapping Java / JSON with Gson

Enjoy! :)

+10
source

I had a problem for up to a month. As far as I remember, it was because, like you, I forgot to do the β€œnew” for the objects. I mean what it should look like:

 public class User { private String timeSpent; private List<WorkLog> worklogs = new List < WorkLog >(); } 

Try this and I hope this helps.

PS Also, as Eric Pragt said, you have an array of users, not just one. Thus, you will need to make another class containing the <Users> list.

0
source

Source: https://habr.com/ru/post/1488306/


All Articles