Reputation:
Getting the MismatchedInputException. Searched a lot of questions here but havent found a solution yet.
Code:
import /path/to/file/Bars;
List<Bars> barResults = null;
public boolean validateData() throws IOException {
boolean flag = false;
try {
if (Data.read() != -1) {
BufferedReader reader = new BufferedReader(new InputStreamReader(Data));
String line;
while ((line = reader.readLine()) != null) {
line = "[{" + line;
System.out.println(line);
ObjectMapper om = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Bars ms = om.readValue(line, Bars.class);
System.out.println(ms);
break;
}
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
Json: //shorten for example
[{"createdOn":1601058721310,"lastUpdated":null,"lastUpdatedBy":null,"createdBy":null,"appId":null,"logical":"N","calculationDateTime":1601058721310,"mtaVersionNumber":null,"storageRegionName":"texas","createdOnDate":1601058721310,"lastUpdatedDate":0}]
Output:
[{"createdOn":1601058721310,"lastUpdated":null,"lastUpdatedBy":null,"createdBy":null,"appId":null,"logical":"N","calculationDateTime":1601058721310,"mtaVersionNumber":null,"storageRegionName":"texas","createdOnDate":1601058721310,"lastUpdatedDate":0}]
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `object` out of START_ARRAY token
at [Source: (StringReader); line: 1, column: 1]
I am not sure what is causing this exception. When I start the application, i runs and reads the JSON fine..but throws exception.
Upvotes: 0
Views: 1838
Reputation: 139
In your call to readValue
you are passing Bars.class
as the second argument, which tells Jackson that the first argument (line
) is a JSON representation of a Bars
instance and that's what it should return.
JSON objects start with a {
, and because you've asked Jackson to deserialize an object, it expects the input to start with a {
. But the JSON that you're passing in, line
, isn't a Bars
instance: it's an array containing a Bars
instance, and it starts with a [
.
So it throws an error message that says "I was told an object would be here, but instead I found the start of an array".
To fix it, you can either ask Jackson to deserialize an array of "Bar" objects by changing the second argument of readValue
to Bars[].class
and then extract the bar instance from the array, or you could stop adding a "[" to the start of the line and chop the "]" off the end of it so that it's just a single object and not an array containing that single object.
Upvotes: 1