Parsing a JSON file in Java using GSON

cksssssss :

I am trying to parse this JSON file using GSON in java but I am running into some difficulties.

{"group": [
{
   "name": "Team1 Student2",
   "email": "ctc-t1-s2",
   "contribution": {
      "score": 16.6,
      "comment": "From 'Team1 Student2' about 'Team1 Student2'\nAnd they said, \"You'll never get it done!\""
   }
}
,{
   "name": "Team1 Student4",
   "email": "mmi-t1-s4",
   "contribution": {
      "score": 0.4,
      "comment": "From 'Team1 Student2' about 'Team1 Student4'\nAnd they said, \"You'll never get it done!\""
   }
}
,{
   "name": "Team1 Student3",
   "email": "pco-t1-s3",
   "contribution": {
      "score": 39.1,
      "comment": "From 'Team1 Student2' about 'Team1 Student3'\nAnd they said, \"You'll never get it done!\""
   }
}
,{
   "name": "Team1 Student1",
   "email": "eyr-t1-s1",
   "contribution": {
      "score": 23.9,
      "comment": "From 'Team1 Student2' about 'Team1 Student1'\nAnd they said, \"You'll never get it done!\""
   }
}],"confidential_comments": "That's all! It was \"fun\"! (said Team1 Student2)\nDon't forget: Bring your towel \\ the cake is a lie."}

Right now I have three classes one of them being a group class:

  public class Group {
   private List<Student> group[];

   public Group(List<Student> students[]){
       this.group =  students;
    }

    public void setStudents(List<Student> students[]) {
        this.group = students;
    }

    public List<Student>[] getStudents() {
        return group;
    }

    @Override
    public String toString() {
        return "Group{" +
                "students=" + Arrays.toString(group) +
                '}';
    }
}

as well as a student class:

public class Student {
    private String name;
    private String sfu_email;

    public Student(String name, String email){
        this.name = name;
        this.sfu_email = email;
    }


    public String getName() {
        return name;
    }

    public String getEmail(){
        return sfu_email;
    }

    public void setEmail(String email) {
        this.sfu_email = email;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String toString(){
        return getName() + " " + getName();
    }
}

And in my main function when I run this I end up printing a NULL object.

 Gson gson = new Gson();

        JsonParser jsonParser = new JsonParser();

        try(Reader reader = new FileReader("FILE PATH")) {
            JsonElement jsonObject = jsonParser.parse((reader));
            System.out.println(jsonObject);

            Group students = gson.fromJson(jsonObject, Group.class);
            System.out.println(students);
            Student student = gson.fromJson(jsonObject, Student.class);
            System.out.println(student);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

I am wondering how would you be able to parse the input file so you would get the JSON string into an object or a list of objects? Sorry for the long question.

Naveen :

Group class contains List<Student>, again not required to add [] at the end of the group variable name.

  public class Group {

    private List<Student> group;

    public void setStudents(List<Student> students) {
        this.group = students;
    }

    public List<Student> getStudents() {
        return group;
    }

    @Override
    public String toString() {
        return "Group [group=" + group + "]";
    }
}

make sure that field names in the classes need to match the JSON.

public class Student {
    private String name;
    private String email; //sfu_email changed to email

    public Student(String name, String email) {
        this.name = name;
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", email=" + email + "]";
    }

try with the above solution.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=356685&siteId=1