简体   繁体   English

使用jackson对一系列空对象进行反序列化的正确方法是什么?

[英]What is the proper way to deserialize an array of of empty objects using jackson?

I'm using Jackson's object mapper in java to deserialize some JSON data. 我在Java中使用Jackson的对象映射器反序列化一些JSON数据。 I have an object with a field that is sometimes empty, but instead of empty I'm recieving an array of empty objects like below. 我有一个对象,该字段的字段有时为空,但不是空的,而是接收到一个空对象的数组,如下所示。

{
    "LineItems" : [
        {},
        {}
    ]
}

Is there a configuration I can set, is there a deserializer I can use to handle this? 是否可以设置配置,是否可以使用解串器来处理?

Based upon Henrik's answer I ended up doing this: 根据Henrik的回答,我最终这样做:

String message = "";//JSON like above
message.replace("{},", "").replace("{}", "").replaceAll("(?m)^[ \t]*\r?\n", "");

Basically replace all instances that have a comma after them, then a single empty value, then empty lines that are left behind. 基本上替换所有在其后带有逗号的实例,然后替换为一个空值,然后替换为空的行。 This solution would require some changes if the input isn't formatted the same as mine, ie comma's in front of the curly braces, or commas on separate lines. 如果输入的格式与我的格式不同(例如,花括号前的逗号或分隔行上的逗号),则此解决方案将需要进行一些更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM