使用Jackson Java库动态更改JsonProperty名称 [英] Dynamic change of JsonProperty name using Jackson java library

查看:469
本文介绍了使用Jackson Java库动态更改JsonProperty名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Jackson 2.9.8将下面的POJO转换为JSON:

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<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   }]
}

但是我需要使用不同的名称对其进行自定义.对于某些响应,应该为busines1business2等.

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

如何使JsonProperty名称动态化?

推荐答案

您可以在构造函数中提供名称,并使用

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<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"
  } ]
}

这篇关于使用Jackson Java库动态更改JsonProperty名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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