com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token – JAVA

huangapple go评论74阅读模式
英文:

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&lt;Bars&gt; 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 = &quot;[{&quot; + 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

[{&quot;createdOn&quot;:1601058721310,&quot;lastUpdated&quot;:null,&quot;lastUpdatedBy&quot;:null,&quot;createdBy&quot;:null,&quot;appId&quot;:null,&quot;logical&quot;:&quot;N&quot;,&quot;calculationDateTime&quot;:1601058721310,&quot;mtaVersionNumber&quot;:null,&quot;storageRegionName&quot;:&quot;texas&quot;,&quot;createdOnDate&quot;:1601058721310,&quot;lastUpdatedDate&quot;:0}]

Output:

[{&quot;createdOn&quot;:1601058721310,&quot;lastUpdated&quot;:null,&quot;lastUpdatedBy&quot;:null,&quot;createdBy&quot;:null,&quot;appId&quot;:null,&quot;logical&quot;:&quot;N&quot;,&quot;calculationDateTime&quot;:1601058721310,&quot;mtaVersionNumber&quot;:null,&quot;storageRegionName&quot;:&quot;texas&quot;,&quot;createdOnDate&quot;:1601058721310,&quot;lastUpdatedDate&quot;: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.

huangapple
  • 本文由 发表于 2020年9月26日 03:09:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64070101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定