使用 Jackson 忽略 JSON 对象上的新字段 [英] Ignoring new fields on JSON objects using Jackson

查看:46
本文介绍了使用 Jackson 忽略 JSON 对象上的新字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jackson JSON 库将一些 JSON 对象转换为 android 应用程序上的 POJO 类.问题是,JSON 对象可能会在发布应用程序时更改并添加新字段,但目前当添加一个简单的 String 字段时它会收支平衡,可以安全地忽略它.

I'm using Jackson JSON library to convert some JSON objects to POJO classes on an android application. 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.

有什么方法可以告诉 Jackson 忽略新添加的字段?(例如,在 POJO 对象上不存在)?全局忽略会很棒.

Is there any way to tell Jackson to ignore newly added fields? (e.g. non-existing on the POJO objects)? A global ignore would be great.

推荐答案

Jackson 提供了一个可以在类级别使用的注解 (JsonIgnoreProperties).

Jackson provides an annotation that can be used on class level (JsonIgnoreProperties).

将以下内容添加到您的类的顶部(不是到个别方法):

Add the following to the top of your class (not to individual methods):

@JsonIgnoreProperties(ignoreUnknown = true)
public class Foo {
    ...
}

根据您使用的 jackson 版本,您必须在当前版本中使用不同的导入:

Depending on the jackson version you are using you would have to use a different import in the current version it is:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

在旧版本中是这样的:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

这篇关于使用 Jackson 忽略 JSON 对象上的新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆