Khalil Jomaa Khalil Jomaa - 1 month ago 7
Android Question

Creating class and parsing JSON array on android

I have the following JSON output example:

[
{
"id": "1",
"first_name": "AAA",
"last name": "BBB",
"courses": [
{
"name": "CCC",
"grade": "90"
},
{
"name": "CCC",
"grade": "90"
},
{
"name": "CCC",
"grade": "90"
}
]
},
{
"id": "2",
"first_name": "AAA",
"last name": "BBB",
"courses": []
},
{
"id": "3",
"first_name": "AAA",
"last name": "BBB",
"courses": [
{
"name": "CCC",
"grade": "90"
},
{
"name": "CCC",
"grade": "90"
}
]
},
{
"id": "3",
"first_name": "AAA",
"last name": "BBB",
"courses": [
{
"name": "CCC",
"grade": "90"
},
{
"name": "CCC",
"grade": "90"
},
{
"name": "CCC",
"grade": "90"
},
{
"name": "CCC",
"grade": "90"
},
{
"name": "CCC",
"grade": "90"
}
]
}
]

I need help with creating class and parsing this JSON.

thanks in advance.

Answer
public class Course {
    private String name;
    private String grade;
}

public class Person {
    private String id;
    private String firstName;
    private String lastName;
    private List<Course> courses = new ArrayList<Course>();
}

this site very helpful in such things - http://www.jsonschema2pojo.org/

Comments