英文:
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: 无法从 START_ARRAY 令牌反序列化 `object` 实例
位于 [Source: (StringReader); line: 1, column: 1]
我不确定是什么导致了这个异常。当我启动应用程序时,它可以正常读取 JSON,但会抛出异常。
英文:
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.
答案1
得分: 1
在你调用 readValue
方法时,你将 Bars.class
作为第二个参数传递,这告诉 Jackson 第一个参数 (line
) 是一个 Bars
实例的 JSON 表示,它应该返回这个实例。
JSON 对象以 {
开始,因为你要求 Jackson 反序列化一个对象,所以它期望输入以 {
开头。但是你传递的 JSON,即 line
,并不是一个 Bars
实例,它是一个包含 Bars
实例的数组,并且以 [
开头。
因此,它会抛出一个错误消息,说“我被告知这里应该是一个对象,但我找到了一个数组的开头”。
要修复这个问题,你可以将 readValue
的第二个参数更改为 Bars[].class
,这样你可以请求 Jackson 反序列化一个 "Bars" 对象的数组,然后从数组中提取 bar 实例。或者你可以停止在 line
的开头添加 [
,并去掉结尾的 ]
,这样它就变成了一个单独的对象,而不是包含单个对象的数组。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论