使用Jackson注解忽略Json中的字段

Json 专栏收录该内容
11 篇文章 0 订阅

使用Jackson注解忽略Json中的字段

Ignore fields in JSON using Jackson @JsonIgnoreProperties or @JsonIgnore

Jackson 提供了一种将JSON绑定到POJO的高效方法。然而,有时候在将JSON转换为Java 对象以及将Java对象转换为JSON时,一些属性需要被忽略。Jackson提供了三种方法来忽略JSON中的字段。

  1. @JsonIgnoreProperties:该注解将在类曾级别上使用以忽略json属性。在下面的栗子中,我们将从albums的dataset中忽略“tag”属性;
  2. @JsonIgnore:该注释将在属性级别上使用以忽略特定属性;
  3. 使用自定义的过滤器;

下面的栗子展示了方法1和方法2,注意@JsonAutoDetect注解的用法;

Jackson中如何忽略字段的示例:

public static void main(String[] args) throws IOException {
	//String url = "http://freemusicarchive.org/api/get/albums.json?api_key=60BLHNQCAOUFPIBZ&limit;
	ObjectMapper mapper = new ObjectMapper();
	String content="{\n" +
			"  \"title\" : \"作者提供的URL访问不了\",\n" +
			"  \"dataset\" : [ {\n" +
			"    \"album_id\" : \"1\",\n" +
			"    \"album_title\" : \"关注一下公众号嘛~\",\n" +
			"    \"tags\" : \"测试\",\n" +
			"    \"支付宝账号\" : \"wethinker_xiaomo@126.com\",\n" +
			"    \"公众号名字\" : \"落花流水存心阁\"\n" +
			"  }, {\n" +
			"    \"album_id\" : \"1\",\n" +
			"    \"album_title\" : \"关注一下公众号嘛~\",\n" +
			"    \"tags\" : \"测试\",\n" +
			"    \"支付宝账号\" : \"wethinker_xiaomo@126.com\",\n" +
			"    \"公众号名字\" : \"落花流水存心阁\"\n" +
			"  } ]\n" +
			"}";
	mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
	AlbumsFilter albums = mapper.readValue(content, AlbumsFilter.class);
	System.out.println(albums.getTitle());
	for (DatasetFilter dataset : albums.getDatasetFilter()) {
		System.out.println(dataset.getAlbum_comments());
		System.out.println(dataset.get("支付宝账号"));
		System.out.println(dataset.get("tags"));
		System.out.println(dataset.get("公众号名字"));
		break;
	}
}
@JsonIgnoreProperties({ "tags" })
public class DatasetFilter {
	private String album_id;
	private String album_title;
	private Map<String , Object> otherProperties = new HashMap<String , Object>();
	private String album_comments;

	@JsonCreator
	public DatasetFilter(@JsonProperty("album_id") String album_id, @JsonProperty("album_title") String album_title) {
		this.album_id = album_id;
		this.album_title = album_title;
	}
	// ignore the property specified by this getter.
	@JsonIgnore
	public String getAlbum_comments() {
		return album_comments;
	}
	public String getAlbum_id() {
		return album_id;
	}
	public void setAlbum_id(String album_id) {
		this.album_id = album_id;
	}
	public String getAlbum_title() {
		return album_title;
	}
	public void setAlbum_title(String album_title) {
		this.album_title = album_title;
	}
	public Object get(String name) {
		return otherProperties.get(name);
	}
	// this method is used to get all properties not specified earlier.
	@JsonAnyGetter
	public Map<String , Object> any() {
		return otherProperties;
	}
	@JsonAnySetter
	public void set(String name, Object value) {
		otherProperties.put(name, value);
	}
}
@JsonAutoDetect(fieldVisibility = Visibility.NONE, getterVisibility = Visibility.PUBLIC_ONLY)
public class AlbumsFilter {
    private String title;
    private DatasetFilter[] datasetFilter;
    public String total_pages;
    protected String getTotal_pages() {
        return total_pages;
    }
    public String getTitle() {
        return title;
    }
    // this getter method is for the 'dataset' property
    @JsonProperty("dataset")
    public DatasetFilter[] getDatasetFilter() {
        return datasetFilter;
    }
}

@JsonIgnoreProperties({ "tags" })
public class DatasetFilter {
    private String album_id;
    private String album_title;
    private Map<String , Object> otherProperties = new HashMap<String , Object>();
    private String album_comments;
    @JsonCreator
    public DatasetFilter(@JsonProperty("album_id") String album_id, @JsonProperty("album_title") String album_title) {
        this.album_id = album_id;
        this.album_title = album_title;
    }
    // ignore the property specified by this getter.
    @JsonIgnore
    public String getAlbum_comments() {
        return album_comments;
    }
    public String getAlbum_id() {
        return album_id;
    }
    public void setAlbum_id(String album_id) {
        this.album_id = album_id;
    }
    public String getAlbum_title() {
        return album_title;
    }
    public void setAlbum_title(String album_title) {
        this.album_title = album_title;
    }
    public Object get(String name) {
        return otherProperties.get(name);
    }
    // this method is used to get all properties not specified earlier.
    @JsonAnyGetter
    public Map<String , Object> any() {
        return otherProperties;
    }
    @JsonAnySetter
    public void set(String name, Object value) {
        otherProperties.put(name, value);
    }
}

译者注:作者提供的URL无法访问,这里使用content代替,序列化时tags属性不会写入JSON;反序列化时也不会从JSON里读取tags属性。

项目源码免费获取 个人博客,百度网盘地址+提取码,文件无密~

  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值