【问题标题】:Jackson - Java Objects with List<Map<String,String>> to Json String conversionJackson - 具有 List<Map<String,String>> 到 Json 字符串转换的 Java 对象
【发布时间】:2020-01-09 10:11:09
【问题描述】:

我想从 Java 对象生成 JSON 字符串

public class Resource {

String name;
List<Item> items;

public String resourceAsJson(Resource resource) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    return objectMapper.writeValueAsString(resource);
}

物品在哪里

public class Item {
    Map<String, String> systemFields;
    Map<String, String> dataFields;
}

此时JSON String的形式是

{
"name": "Person",
"items": [
    {
        "systemFields": {
            "systemField1": "xxx",
            "systemField2": "xxx",
            "systemField3": "x"
        },
        "dataFields": {
            "dataField1": "xxx",
            "dataField2": "xxx",
            "dataField3": "x"
        }
    }
    ]
}

我试图获得的是 JSON 的不同形式(省略 Item 并在一个 Json 表中有“系统字段”“数据字段”)

{
"Person":[
        {
        "systemField1": "xxx",
        "systemField2": "xxx",
        "systemField3": "Warsaw",
        "dataField1": "xxx",
        "dataField2": "xxx",
        "dataField3": "xxx"
        }
    ]
}

有没有办法在不改变模型的情况下对 Jackson 做到这一点?

【问题讨论】:

  • @JsonUnwrapped 可能就是你要找的东西
  • 你能把整个代码块发给我,这样我就可以在本地工作了。

标签: java json spring solr jackson


【解决方案1】:

在这种情况下,POJO 的默认表示不是您想要的,您需要实现自定义序列化程序。在您的情况下,它们可能如下所示:

class ResourceJsonSerializer extends JsonSerializer<Resource> {
    @Override
    public void serialize(Resource value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeStartObject();
        gen.writeFieldName(value.getName());
        gen.writeObject(value.getItems());
        gen.writeEndObject();
    }
}

class ItemJsonSerializer extends JsonSerializer<Item> {

    @Override
    public void serialize(Item value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeStartObject();
        writeMap(value.getSystemFields(), gen);
        writeMap(value.getDataFields(), gen);
        gen.writeEndObject();
    }

    private void writeMap(Map<String, String> map, JsonGenerator gen) throws IOException {
        if (map != null) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                gen.writeStringField(entry.getKey(), entry.getValue());
            }
        }
    }
}

你可以使用com.fasterxml.jackson.databind.annotation.JsonSerialize注解注册它们:

@JsonSerialize(using = ResourceJsonSerializer.class)
class Resource {

和:

@JsonSerialize(using = ItemJsonSerializer.class)
class Item {

【讨论】:

  • 谢谢。这正是我想做的!
  • 有什么选项可以只在我想使用的时候使用吗?我的意思是“按需”以这种方式序列化。我问是因为应用程序将此模型保存到 JSON 几次,但只有一次我想要“简化”一个。
  • @PiotrSupel,以question 为例。在我的回答中,您可以找到 SimpleModule 对象。您可以使用它注册自定义序列化程序,并仅在需要序列化程序时为ObjectMapper 注册它。另请参阅:Deserialization of java.utils.logging.Level class with Jackson returns null
猜你喜欢
  • 2011-02-01
  • 2018-05-08
  • 2017-07-18
  • 1970-01-01
  • 1970-01-01
  • 2018-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多