java jsonproperty,使用Jackson Java库动态更改JsonProperty名称

I use Jackson 2.9.8 for converting my below POJO as JSON:

public class ResponseEntity implements Serializable {

private static final long serialVersionUID = 1L;

private int total_record_count;

private int filtered_record_count;

@JsonProperty("list")

private List> entityList;

public ResponseEntity(List> entityList) {

this.entityList = entityList;

this.filtered_record_count = entityList.size();

}

public int getTotal_record_count() {

return total_record_count;

}

public void setTotal_record_count(int total_record_count) {

this.total_record_count = total_record_count;

}

public int getFiltered_record_count() {

return filtered_record_count;

}

public void setFiltered_record_count(int filtered_record_count) {

this.filtered_record_count = filtered_record_count;

}

public List> getEntityList() {

return entityList;

}

public void setEntityList(List> entityList) {

this.entityList = entityList;

}

}

In the result JSON, value of entityList member is mapped to list key as it's annotated with @JsonProperty("list"):

{

"list" : [ {

"id" : "IID000000002129959",

"attr1" : "MY",

"attr2" : "sd",

"attr3" : true }]

}

But I need to customise it with different names. For some response it should be busines1, business2, etc.

How do I make JsonProperty name dynamic?

解决方案

You can provide name in constructor and use JsonAnyGetter. Below solution:

import com.fasterxml.jackson.annotation.JsonAnyGetter;

import com.fasterxml.jackson.annotation.JsonIgnore;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.Serializable;

import java.util.Collections;

import java.util.List;

import java.util.Map;

public class JsonApp {

public static void main(String[] args) throws Exception {

ResponseEntity entity = new ResponseEntity("dynList",

Collections.singletonList(Collections.singletonMap("key", "value1")));

ObjectMapper mapper = new ObjectMapper();

mapper.enable(SerializationFeature.INDENT_OUTPUT);

System.out.println(mapper.writeValueAsString(entity));

}

}

class ResponseEntity implements Serializable {

private static final long serialVersionUID = 1L;

private int total_record_count;

private int filtered_record_count;

private String propertyName;

@JsonIgnore

private List> entityList;

public ResponseEntity(String propertyName, List> entityList) {

this.propertyName = propertyName;

this.entityList = entityList;

this.filtered_record_count = entityList.size();

}

@JsonAnyGetter

public Map otherProperties() {

return Collections.singletonMap(propertyName, entityList);

}

// other methods

}

prints:

{

"total_record_count" : 0,

"filtered_record_count" : 1,

"dynList" : [ {

"key" : "value1"

} ]

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值