简体   繁体   中英

Deserializing JSON to Java using Jackson

I am working with JSON for the very first time and I am trying to deserialise a JSON file into Java objects.

InputJSON

{

    "student_id" : "123",
     "courses":[
        {
         "course_id":"789",
         "professor":"abc"
        }
      ]
}
{
    "student_id":"234",
     courses:[
       {
         "course_id":"789",
         "professor":"pqr"
       },
       {
         "course_id":"789",
         "professor":"xyz"
       }
     ]
}
{
   "student_id" : "345",
     "courses":[
        {
         "course_id":"567",
         "professor":"lmn"
        }
      ]
}

Student.class

class Student {

   @JsonProperty("student_id")
   private String studentId;
   @JsonProperty("courses")
   private List<Courses> courses;

   //getters and setters

}

Courses.class

public class Courses {

    @JsonProperty("course_id")
    private String courseId;
    @JsonProperty("professor")
    private String professor;

    //getters and setters
}

My JsonHelper.class creates an object of Object Mapper class and uses readValue(new File("Input.json"),Student.class) to to map the json fields to Java objects.

What I want to do is create a map with studentId as a key and the list of courses as the value corresponding to each studentId.

I am not exactly getting how to achieve it since my list is just able to get 1 element and the list size is thus 1. And issue with creating the map too. Any help will be appreciated.

This java class seems wrong. because your variable name is not the same with your JSON object.

public class Courses {
    @JsonProperty("course_id")
    private String courseId;
    @JsonProperty("professor")
    private String professor;
    //getters and setters
}

it would be like this,

class Student {
   @JsonProperty("student_id")
   private String student_id;
   @JsonProperty("courses")
   private List<Courses> courses;
   //getters and setters

    }
    public class Courses {
            @JsonProperty("course_id")
            private String course_id;
            @JsonProperty("professor")
            private String professor;
            //getters and setters
        }

Here are some ways to do mapping with ObjectMapper:

ObjectMapper objectMapper = new ObjectMapper();

For parsing a single Student object:

Student student = objectMapper.readValue(inputJSONString, Student.class);

For parsing a list of Student object which is what you want:

List<Student> students = objectMapper.readValue(inputJSONString, objectMapper.getTypeFactory().constructCollectionType(List.class, Student.class));

For parsing a maps:

Map<String, Courses> maps = objectMapper.readValue(inputJSONString, objectMapper.getTypeFactory().constructMapType(Map.class, String.class, Courses.class));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM