Java Jackson readTree()忽略空值和空值
我也看到过一些类似的帖子,所以如果有人问过我,我会提前道歉。然而,我无法找到任何适合我的解决方案 我从一个web服务中获得了一块JSON,然后我想把它保存在Amazon的DynamoDB中。然而,返回的JSON中有一些null和空值,这是DynamoDB不喜欢的 所以我想从JSON中删除这些值。我目前通过readTree()方法使用Jackson进行JSON反序列化,如下所示:Java Jackson readTree()忽略空值和空值,java,json,jackson,Java,Json,Jackson,我也看到过一些类似的帖子,所以如果有人问过我,我会提前道歉。然而,我无法找到任何适合我的解决方案 我从一个web服务中获得了一块JSON,然后我想把它保存在Amazon的DynamoDB中。然而,返回的JSON中有一些null和空值,这是DynamoDB不喜欢的 所以我想从JSON中删除这些值。我目前通过readTree()方法使用Jackson进行JSON反序列化,如下所示: String sitesDataJson = webService.getData(); // create the
String sitesDataJson = webService.getData();
// create the object mapper and instruct it to skip null values
ObjectMapper mapper = new ObjectMapper();
mapper = mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
JsonNode jsonObj = null;
try {
jsonObj = mapper.readTree(sitesDataJson);
} catch (IOException e) {
e.printStackTrace();
}
{
"domain_override":"",
"id":"106949",
"category_override":null,
"content_topic":null,
"modified_date":"2013-12-04 20:31:50",
"market":{
"brand_labels":{
"op":"allow_all"
},
"domains":null,
"blocked_contentattributes":{
"6":true
},
"blocked_creativetypes":{
"11":true,
"10":true,
"5":true
},
"brands":{
"ids":{
"5953":true,
"4644":true,
"8418":true,
"25480":true,
"95":true,
"5650":true
},
"op":"block"
},
"blocked_languages":{
},
"allow_unbranded_buyers":"0"
},
"type":"site",
"status":"Active",
"account_id":"100766",
"deleted":"0",
"v":"2",
"delivery_medium":"WEB",
"delivery_medium_id":"2",
"content_type_id":null,
"notes":null,
"platform_id":null,
"autorefresh_settings":{
},
"created_date":"2013-10-16 16:49:48",
"external_id":null,
}
我试图阅读的JSON如下所示:
String sitesDataJson = webService.getData();
// create the object mapper and instruct it to skip null values
ObjectMapper mapper = new ObjectMapper();
mapper = mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
JsonNode jsonObj = null;
try {
jsonObj = mapper.readTree(sitesDataJson);
} catch (IOException e) {
e.printStackTrace();
}
{
"domain_override":"",
"id":"106949",
"category_override":null,
"content_topic":null,
"modified_date":"2013-12-04 20:31:50",
"market":{
"brand_labels":{
"op":"allow_all"
},
"domains":null,
"blocked_contentattributes":{
"6":true
},
"blocked_creativetypes":{
"11":true,
"10":true,
"5":true
},
"brands":{
"ids":{
"5953":true,
"4644":true,
"8418":true,
"25480":true,
"95":true,
"5650":true
},
"op":"block"
},
"blocked_languages":{
},
"allow_unbranded_buyers":"0"
},
"type":"site",
"status":"Active",
"account_id":"100766",
"deleted":"0",
"v":"2",
"delivery_medium":"WEB",
"delivery_medium_id":"2",
"content_type_id":null,
"notes":null,
"platform_id":null,
"autorefresh_settings":{
},
"created_date":"2013-10-16 16:49:48",
"external_id":null,
}
使用非空或非空设置执行readTree()似乎没有什么区别。使用.toString()打印jsonObj会显示仍然存在的空值和空值
那么我是不是走错了路?有没有更好的(或正确的)方法
我还应该补充一点,我也尝试过这样做,但是使用GSON(我读过的GSON)默认情况下会删除这些属性。然而,这也不能消除它们。那么,这是否表明所使用的JSON中存在一些奇怪之处
但我不明白这是怎么回事,因为我尝试用一点非常简单的JSON测试我的代码(Jackson和GSON):
String json = "{"
+ "\"name\" : \"val1\","
+ "\"address\" : null"
+ "}";
但在使用readTree()时,这仍然不会删除address属性。这是否意味着非空和非空设置对readTree()方法没有影响?事实上,它们是否仅用于将对象序列化为JSON?我真的需要将JSON读入POJO才能处理null或空值吗
非常感谢。我不确定我提出的解决方案是否100%适用于您的案例,但有一种方法可以在反序列化过程中使用自定义反序列化程序
StringDeserializer deserializer = new StringDeserializer();
SimpleModule module = new SimpleModule("StringDeserializerModule",
new Version(1, 0, 0, null));
module.addDeserializer(String.class, deserializer);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
现在,您可以为您认为可能存在null/empty的属性添加更多自定义Deserializer,并自定义deserialize()表示
class StringDeserializer extends JsonDeserializer<String>
{
@Override
public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException
{
//Customize
}
}
类StringDeserializer扩展JsonDeserializer
{
@凌驾
公共字符串反序列化(JsonParser jp,DeserializationContext ctxt)引发IOException,JsonProcessingException
{
//定制
}
}
您使用的Jackson库的版本是什么?我使用的是Jackson的2.5.1版。在我看来,null
值可以,但空字符串(或数组)不行。@AnindaBhattacharyya给出的解决方案对我有效。很高兴听到这个消息。那么你能接受这个答案吗?