I'm trying to map JSON to ArrayList and I'm getting "Cannot deserialize instance of java.util.ArrayList
out of START_OBJECT token" error and I don't understand why. I found this article: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token resolving similar problem (there was invalid JSON), but it seems like I have valid json file. I'm using maping method presented in this question: How to use Jackson to deserialise an array of objects
I found this article: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token resolving similar problem (there was invalid JSON), but it seems like I have valid json file. I'm using maping method presented in this question: How to use Jackson to deserialise an array of objects
My Json looks like this:
[
{
"id": "12345",
"name": "John"
},
{
"id": "09876",
"name": "Desmond"
}
]
Data model:
public class Student {
private int id;
private String name;
public Student() {}
// getters, setters and tostring
}
Parsing code:
ObjectMapper objectMapper = new ObjectMapper();
Path pathToStudentsJson = Paths.get("src/main/resources/static/students.json");
File studentsFile = new File(pathToSingleStudentJson.toString());
List<Student> listOfStudents = objectMapper.readValue(studentsFile, new TypeReference<List<Student>>(){});
The error, that I'm getting:
Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
at [Source: (File); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1139)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1093)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:332)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:265)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2940)
at io.json.JsonApp.main(JsonApp.java:46)
Process finished with exit code 1