[英]Jackson deserialize single item into list
我正在尝试使用一个服务,该服务为我提供了一个实体,该实体的字段是一个数组。
{
"id": "23233",
"items": [
{
"name": "item 1"
},
{
"name": "item 2"
}
]
}
但是当数组包含单个项目时,将返回项目本身,而不是一个元素的数组。
{
"id": "43567",
"items": {
"name": "item only"
}
}
在这种情况下,Jackson 无法转换为我的 Java 对象。
public class ResponseItem {
private String id;
private List<Item> items;
//Getters and setters...
}
有没有直接的解决方案?
您不是第一个提出这个问题的人。 好像还挺旧的。
看完这个问题,可以使用DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY
:
查看文档: http : //fasterxml.github.io/jackson-databind/javadoc/2.5/com/fasterxml/jackson/databind/DeserializationFeature.html#ACCEPT_SINGLE_VALUE_AS_ARRAY
您需要将此 jackson 功能添加到您的对象映射器中。
我希望它会帮助你。
我认为注释更适合代码。
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
private List<Item> items;
可以使用自定义 JsonDeserializer 来解决此问题。
例如
class CustomDeserializer extends JsonDeserializer<List<Item>> {
@Override
public List<Item> deserialize(JsonParser jsonParser, DeserializationContext context)
throws IOException, JsonProcessingException {
JsonNode node = jsonParser.readValueAsTree();
List<Item> items = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
if (node.size() == 1) {
Item item = mapper.readValue(node.toString(), Item.class);
items.add(item);
} else {
for (int i = 0; i < node.size(); i++) {
Item item = mapper.readValue(node.get(i).toString(), Item.class);
items.add(item);
}
}
return items;
}
}
您需要告诉 jackson 使用它来反序列化items
,例如:
@JsonDeserialize(using = CustomDeserializer.class)
private List<Item> items;
之后它将起作用。 快乐编码:)
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
似乎不适用于@JsonValue
。
这意味着如果您想将其中任何一个反序列化为列表,它将不起作用:
{
"key": true
}
[
{
"key": true
},
{
"key": false
}
]
作为解决方案,可以像这样创建自定义解串器:
@JsonDeserialize(using = CustomDeserializer.class)
public record ListHolder(List<Item> items) {}
public class CustomDeserializer extends JsonDeserializer<ListHolder> {
@Override
public LogRequest deserialize(JsonParser parser, DeserializationContext context) throws IOException {
List<Item> items;
if (parser.isExpectedStartArrayToken()) {
items = parser.readValueAs(new TypeReference<>() {});
} else {
items = List.of(parser.readValueAs(Item.class));
}
return new ListHolder(items);
}
}
我很漂亮Gson能够在没有打嗝的情况下做到这一点。 希望有人可以帮助你,如果你坚持使用杰克逊。
(例如: https : //stackoverflow.com/a/18547661/1638595 )
编辑:嘿,对不起意思是说:“希望有人可以帮助你......”而不是“不能”。 显得很粗鲁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.