com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_OBJECT 令牌中反序列化 `java.util.Arra
Posted
技术标签:
【中文标题】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 【发布时间】:2020-02-20 16:37:39 【问题描述】:如何使用Jackson
ObjectMapper
阅读下面的JSON
?我已经开发了代码,但遇到了错误。
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"
]
【问题讨论】:
请显示minimal reproducible example 与您的Java 实体和对ObjectMapper
的反序列化调用。
【参考方案1】:
您需要启用ACCEPT_SINGLE_VALUE_AS_ARRAY 功能。可能在POJO
中你有一个List
,但是当List
中只有一个元素时,JSON
有效负载生成时没有数组括号。
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.Arra的主要内容,如果未能解决你的问题,请参考以下文章