Search code examples
javaspring-mvcjacksonresttemplatepojo

Jackson mapping JSON to Java POJO - MismatchedInstanceException


I´m using spring rest template and want to parse the JSON response into the corresponding java POJO. I get every time a MismatchedInputException - Cannot deserialized instance of out of start array token.

I´ve looked at several questions relating to jackson but didn´t find the suitable solution. I´m not sure if my POJO structure is correct.

First the related JSON:

{
   "days":[
      [
         {
            "day":"2019-04-29",
            "time":"08:00-09:30",
            "room":"room1",
            "name":"lecture1",
            "lecturer":"prof1",
            "eventId":332713,
            "modified":false
         }
      ],
      [

      ],
      [

      ],
      [
         {
            "day":"2019-05-02",
            "time":"08:00-10:15",
            "room":"room2",
            "name":"lecture2",
            "lecturer":"prof2",
            "eventId":332714,
            "modified":false
         },
         {
            "day":"2019-05-02",
            "time":"10:45-13:00",
            "room":"room3",
            "name":"lecture3",
            "lecturer":"prof3",
            "eventId":332721,
            "modified":false
         }
      ],
      [

      ],
      [

      ],
      [
      ]
   ]
}

POJO Structure:

public class Main{

    ArrayList<Day> days = new ArrayList<>();

    public ArrayList<Day> getDays() {
        return days;
    }

    public void setDays(ArrayList<Day> days) {
        this.days = days;
    }

    public class Day {

        private String day;
        private String time;
        private String room;
        private String name;
        private String lecturer;
        private Integer eventId;
        private Boolean modified;

        public Day() {
        }
       // Getter and setter of Day Object
    }
}

Rest-Template Method in API Object:


public String getInfoByEventsPeriods(String eventIDs) {
        String url = APIURL + "/info?from="2019-04-29"&to="2019-05-03"&eventId=" + eventIDs;

        return rest.getForObject(url, String.class);
    }

Processing with ObjectMapper in Main Object:

public Main getLectures(String eventIDs) throws IOException {
        API api = new API();
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(api.getInfoByEventsPeriods(eventIDs), Main.class);

    }

Solution

  • Can you have your POJO like below. I tried to deserialize with your JSON and I was able to deserialize successfully.

    class Day {
    
      private String day;
      private String time;
      private String room;
      private String name;
      private String lecturer;
      private Integer eventId;
      private Boolean modified;
    
      public void setDay(String day) {
        this.day = day;
      }
    
      public void setTime(String time) {
        this.time = time;
      }
    
      public void setRoom(String room) {
        this.room = room;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    
      public void setLecturer(String lecturer) {
        this.lecturer = lecturer;
      }
    
      public void setEventId(Integer eventId) {
        this.eventId = eventId;
      }
    
      public void setModified(Boolean modified) {
        this.modified = modified;
      }
    }
    
    class Main{
      private List<List<Day>> days;
      public void setDays(List<List<Day>> days) {
        this.days = days;
      }
    }
    

    enter image description here