com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_ARRAY 令牌反序列化对象实例 - JAVA
com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token - JAVA
正在获取 MismatchedInputException。在这里搜索了很多问题,但还没有找到解决方案。
代码:
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://缩短例如
[{"createdOn":1601058721310,"lastUpdated":null,"lastUpdatedBy":null,"createdBy":null,"appId":null,"logical":"N","calculationDateTime":1601058721310,"mtaVersionNumber":null,"storageRegionName":"texas","createdOnDate":1601058721310,"lastUpdatedDate":0}]
输出:
[{"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]
我不确定是什么导致了这个异常。当我启动应用程序时,我运行并读取 JSON 正常..但抛出异常。
在您对 readValue
的调用中,您将 Bars.class
作为第二个参数传递,它告诉 Jackson 第一个参数 (line
) 是 JSON 的表示一个 Bars
实例,这就是它应该 return.
JSON 对象以 {
开头,因为您要求 Jackson 反序列化一个对象,它期望输入以 {
开头。但是您传入的 JSON line
不是 Bars
实例:它是一个包含 Bars
实例的数组,它以 [
.
因此它会抛出一条错误消息,指出“有人告诉我这里有一个对象,但我却找到了数组的开头”。
要修复它,您可以要求 Jackson 通过将 readValue
的第二个参数更改为 Bars[].class
来反序列化“Bar”对象数组,然后从数组中提取 bar 实例,或者您可以停止在行首添加“[”并将行尾的“]”去掉,这样它就只是一个对象而不是包含该单个对象的数组。
正在获取 MismatchedInputException。在这里搜索了很多问题,但还没有找到解决方案。
代码:
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://缩短例如
[{"createdOn":1601058721310,"lastUpdated":null,"lastUpdatedBy":null,"createdBy":null,"appId":null,"logical":"N","calculationDateTime":1601058721310,"mtaVersionNumber":null,"storageRegionName":"texas","createdOnDate":1601058721310,"lastUpdatedDate":0}]
输出:
[{"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]
我不确定是什么导致了这个异常。当我启动应用程序时,我运行并读取 JSON 正常..但抛出异常。
在您对 readValue
的调用中,您将 Bars.class
作为第二个参数传递,它告诉 Jackson 第一个参数 (line
) 是 JSON 的表示一个 Bars
实例,这就是它应该 return.
JSON 对象以 {
开头,因为您要求 Jackson 反序列化一个对象,它期望输入以 {
开头。但是您传入的 JSON line
不是 Bars
实例:它是一个包含 Bars
实例的数组,它以 [
.
因此它会抛出一条错误消息,指出“有人告诉我这里有一个对象,但我却找到了数组的开头”。
要修复它,您可以要求 Jackson 通过将 readValue
的第二个参数更改为 Bars[].class
来反序列化“Bar”对象数组,然后从数组中提取 bar 实例,或者您可以停止在行首添加“[”并将行尾的“]”去掉,这样它就只是一个对象而不是包含该单个对象的数组。