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

查看:944
本文介绍了使用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.

有没有办法告诉杰克逊忽略新添加的字段? (例如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;

旧版本中的

in older versions it has been:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

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

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