Skip to content Skip to sidebar Skip to footer

How To Parse Nested Json With Gson

Lets say i have a JSON like: { 'assignments': [ { 'id': '111', 'activities': [ { 'activity': 'Activity 1',

Solution 1:

I'd create another Bean for Activities since it is a JSON object in itself.

classAssignment {

    private String id;
    private List<Activity> activities; //getters and setters for this.public String getId() {
        return id;
    }

}

classActivity {
    private String activity; //Getters and setters
}


Gson mGson= new Gson();
assignmentList=mGson.fromJson(json, AssignmentList.class);
assignmentList.getAssignments().get(0).getActivities.get(1);

Solution 2:

If there are only primitive types in the class, just defining two Java classes should be enough, just make sure not to inherit the class from GenericJson, it breaks the recursive operation of a Gson parser.

Post a Comment for "How To Parse Nested Json With Gson"