java - 使用 Jackson java 库动态更改 JsonProperty 名称
问题描述
我Jackson 2.9.8
用于将以下内容转换POJO
为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<Map<String,Object>> entityList;
public ResponseEntity(List<Map<String,Object>> 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<Map<String, Object>> getEntityList() {
return entityList;
}
public void setEntityList(List<Map<String, Object>> entityList) {
this.entityList = entityList;
}
}
结果JSON
,entityList成员的值被映射到列表键,因为它被注释@JsonProperty("list")
:
{
"list" : [ {
"id" : "IID000000002129959",
"attr1" : "MY",
"attr2" : "sd",
"attr3" : true }]
}
但我需要用不同的名字来定制它。对于某些响应,它应该是busines1
,business2
等。
如何使JsonProperty
名称动态化?
解决方案
您可以在构造函数中提供名称并使用JsonAnyGetter
. 以下解决方案:
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<Map<String, Object>> entityList;
public ResponseEntity(String propertyName, List<Map<String, Object>> entityList) {
this.propertyName = propertyName;
this.entityList = entityList;
this.filtered_record_count = entityList.size();
}
@JsonAnyGetter
public Map<String, Object> otherProperties() {
return Collections.singletonMap(propertyName, entityList);
}
// other methods
}
印刷:
{
"total_record_count" : 0,
"filtered_record_count" : 1,
"dynList" : [ {
"key" : "value1"
} ]
}
推荐阅读
- python-3.x - 如何使用 tkinter 嵌入式 matplotlib 图中的按钮在两个图之间切换?
- git - 没有主分支的“GitLab Flow”
- wordpress - 登录/注销按钮过滤器
- excel - 使用 DataFrames 每天将数据记录到 Excel 中
- python - 期望从视图返回一个 `Response`、`HttpResponse` 或 `HttpStreamingResponse`,但收到了一个`
` - ruby-on-rails - 如何使用 Ruby 将 csv 文件的内容写入电子邮件?
- javascript - 如何在 webdriverio 中编写自定义命令
- jupyter-notebook - 如何在 Jupyter 笔记本中运行命令?
- c++ - 一种避免 pgm_read 访问闪存的方法(avr 微控制器)
- c++ - 我在使用 Code::blocks 上的编译器标志时遇到问题。编程初学者