使用Jackson忽略JSON对象上的新字段[复制]

本文翻译自:Ignoring new fields on JSON objects using Jackson [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

I'm using Jackson JSON library to convert some JSON objects to POJO classes on an android application. 我正在使用Jackson JSON库将一些JSON对象转换为Android应用程序上的POJO类。 The problem is, the JSON objects might change and have new fields added while the application is published, but currently it will break even when a simple String field is added, which can safely be ignored. 问题是,JSON对象可能会更改并在应用程序发布时添加新字段,但是当添加一个简单的String字段时,它可能会中断,这可以安全地忽略。

Is there any way to tell Jackson to ignore newly added fields? 有没有办法告诉杰克逊忽略新添加的字段? (eg non-existing on the POJO objects)? (例如,POJO对象上不存在)? A global ignore would be great. 全球忽视将是伟大的。


#1楼

参考:https://stackoom.com/question/Mt66/使用Jackson忽略JSON对象上的新字段-复制


#2楼

确保将@JsonIgnoreProperties(ignoreUnknown = true)注释放置到要作为解析JSON响应而不是从JSON转换为Java Object的类而要填充的父POJO类中。


#3楼

Up to date and complete answer with Jackson 2 杰克逊2的最新和完整答案


Using Annotation 使用注释

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyMappingClass {

}

See JsonIgnoreProperties on Jackson online documentation. 请参阅Jackson在线文档中的JsonIgnoreProperties

Using Configuration 使用配置

Less intrusive than annotation. 比注释更少侵入性。

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

ObjectReader objectReader = objectMapper.reader(MyMappingClass.class);
MyMappingClass myMappingClass = objectReader.readValue(json);

See FAIL_ON_UNKNOWN_PROPERTIES on Jackson online documentation. 请参阅Jackson在线文档中的FAIL_ON_UNKNOWN_PROPERTIES


#4楼

As stated above the annotations only works if this is specified in the parent POJO class and not the class where the conversion from JSON to Java Object is taking place. 如上所述,注释仅在父POJO类中指定,而不是从JSON转换为Java Object的类时才有效。

The other alternative without touching the parent class and causing disruptions is to implement your own mapper config only for the mapper methods you need for this. 另一种不触及父类并导致中断的替代方法是仅为您需要的映射器方法实现自己的映射器配置。

Also the package of the Deserialization feature has been moved. 此外,已移动反序列化功能的包。 DeserializationConfig.FAIL_ON_UNKNOWN_PROPERTIES to DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES DeserializationConfig.FAIL_ON_UNKNOWN_PROPERTIES to DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES

import org.codehaus.jackson.map.DeserializationConfig;
...
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

#5楼

If using a pojo class based on JSON response. 如果使用基于JSON响应的pojo类。 If chances are there that json changes frequently declare at pojo class level: 如果有可能json更改经常在pojo类级别声明:

@JsonIgnoreProperties(ignoreUnknown = true)

and at the objectMapper add this if you are converting: 如果要转换,则在objectMapper中添加:

objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

So that code will not break. 所以代码不会破坏。


#6楼

Starting with Jackson version 2.4 and above there have been some changes. 从Jackson 2.4及更高版本开始,有一些变化。 Here is how you do it now: 以下是您现在的工作方式:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

.......................................................................... .................................................. ........................

 ObjectMapper mapper = new ObjectMapper();
    // to prevent exception when encountering unknown property:
 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

Note: The @annotation based solution remains the same so if you like to use that see the other answers. 注意:基于@注释的解决方案保持不变,因此如果您想使用它,请参阅其他答案。

For more information see the 10 minutes Configuration tutorial at: https://github.com/FasterXML/jackson-databind For more information see the 10 minutes Configuration tutorial at: https For more information see the 10 minutes Configuration tutorial at: //github.com/FasterXML/jackson-databind

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值