java - 使用 Jackson java 库动态更改 JsonProperty 名称

标签 java json serialization jackson jackson-databind

我使用 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成员的值映射到list 键,因为它用 @JsonProperty("list") 注释:

{
   "list" : [ {
     "id" : "IID000000002129959",
     "attr1" : "MY",
     "attr2" : "sd",
     "attr3" : true   }]
}

但我需要用不同的名称来自定义它。对于某些响应,它应该是 business1business2 等。

如何使 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"
  } ]
}

关于java - 使用 Jackson java 库动态更改 JsonProperty 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55845478/

相关文章:

java - 驯服 Java 泛型中的类型检查器

java - 我如何在 Tomcat Servlet 中打印一些东西, hibernate 3 秒然后转发到另一个 servlet?

c# - json.net 列表序列化到 JSON 数组

Java序列化问题

java - Windows JVM 命令行参数上的尾随星号在 cygwin bash shell 中被通配

java - 使用 powermockito 验证静态 void 方法的调用

javascript - jquery中从json数组中分割json数据

Java JSON 序列化和 JSONObject

php - Symfony 序列化程序不会反序列化为不同命名空间 : 'no supporting normalizer found' 中的对象

c# - JSON.NET 根标记和反序列化