com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_OBJECT 令牌反序列化 `java.util.ArrayList` 的实例 [英] com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
本文介绍了com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_OBJECT 令牌反序列化 `java.util.ArrayList` 的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 Jackson
ObjectMapper
阅读下面的 JSON
?我已经开发了代码,但出现以下错误.
How to read below JSON
using Jackson
ObjectMapper
? I have developed code but getting below error.
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
at [Source: (File); line: 7, column: 19] (through reference chain: com.example.demo.resources.Orgnization["secondaryIds"])
JSON
{
"id": "100000",
"name": "ABC",
"keyAccount": false,
"phone": "1111111",
"phoneExtn": "11",
"secondaryIds": {
"ROP": [
"700010015",
"454546767",
"747485968",
"343434343"
],
"AHID": [
"01122006",
"03112001"
]
}
}
推荐答案
您需要启用 ACCEPT_SINGLE_VALUE_AS_ARRAY 功能.可能在 POJO
中你有一个 List
但是当 List
中只有一个元素时 JSON
有效载荷生成时没有数组括号.
You need to enable ACCEPT_SINGLE_VALUE_AS_ARRAY feature. Probably in POJO
you have a List
but when there is only one element in a List
JSON
payload is generated without array brackets.
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.List;
public class JsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./src/main/resources/test.json");
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
Orgnization root = mapper.readValue(jsonFile, Orgnization.class);
System.out.println(root);
}
}
这篇关于com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_OBJECT 令牌反序列化 `java.util.ArrayList` 的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文