簡體   English   中英

保持 POJO 完整如何使用 Jackson 在 JSON 中注入新字段?

[英]Keeping POJO intact how to inject new field in JSON using Jackson?

我的最終目標是,我有POJO 列表 我需要使用 Jackson 轉換為JSON 字符串 轉換為 JSON 時。 我需要添加更多的鍵。 為了做到這一點,我只有解決方案。 如果我將POJO列表轉換為ObjectNode 列表,以便我可以在ObjectNode 中添加信息。 之后我將轉換為JSON String

List<dummyPOJO> dummyPOJO = getPOJO(); ObjectNode objectNode = mapper.convertValue(dummyPOJO, new TypeReference<ArrayList<ObjectNode>>(){});

此代碼給出了一個跟隨錯誤

java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode

如果有任何可用的解決方案,請告訴我。 提前致謝 :)

以下代碼看起來不正確

ObjectNode objectNode = mapper.convertValue(dummyPOJO, new TypeReference<List<ObjectNode>>(){});

它應該失敗

Type mismatch: cannot convert from List<ObjectNode> to ObjectNode

它可以更新為

List<ObjectNode> objectNodes = mapper.convertValue(dummyPOJO, new TypeReference<List<ObjectNode>>(){});

在不更新 POJO 的情況下,要在 json 中添加額外的字段,您可以使用以下過程

這是一個簡單的 POJO,有 3 個字段 firstName、lastName 和 age

人.java

package sep2020;

public class Person {
    public String firstName;
    public String lastName;
    public int age;

    public Person() {
    }

    public Person(String firstName, String lastName, int age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    public String toString() {
        return "[" + firstName + " " + lastName + " " + age + "]";
    }
}

現在將創建一些 Person 對象並將它們轉換為List<ObjectNode>結構。

然后將遍歷所有 ObjectNode 並在 Person ObjectNode 中添加新的字段 Sex。

但 POJO 結構將保持不變。

JSONListConverter.java

package sep2020;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class JSONListConverter {
    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

        // Define map which will be converted to JSON
        List<Person> personList = Stream.of(new Person("A", "B", 34),
                new Person("C", "D", 75), new Person("E", "F", 21),
                new Person("G", "H", 55)).collect(Collectors.toList());

        List<ObjectNode> objectNodes = objectMapper.convertValue(personList,
                new TypeReference<List<ObjectNode>>() {
                });

        JsonNode jsonNode = objectMapper.convertValue(objectNodes,
                JsonNode.class);
        // Existing Json structure
        System.out.println("Existing Json:\n"
                + objectMapper.writeValueAsString(jsonNode));
        System.out.println("\n\n");
        // Adding an extra field
        if (objectNodes != null && objectNodes.size() > 0) {
            for (ObjectNode objectNode : objectNodes) {
                objectNode.put("Sex", "M");
            }
        }
        jsonNode = objectMapper.convertValue(objectNodes, JsonNode.class);
        // Updated Json structure
        System.out.println("Updated Json:\n"
                + objectMapper.writeValueAsString(jsonNode));
    }
}

輸出:

Existing Json:

[ {
  "firstName" : "A",
  "lastName" : "B",
  "age" : 34
}, {
  "firstName" : "C",
  "lastName" : "D",
  "age" : 75
}, {
  "firstName" : "E",
  "lastName" : "F",
  "age" : 21
}, {
  "firstName" : "G",
  "lastName" : "H",
  "age" : 55
} ]



Updated Json:

[ {
  "firstName" : "A",
  "lastName" : "B",
  "age" : 34,
  "Sex" : "M"
}, {
  "firstName" : "C",
  "lastName" : "D",
  "age" : 75,
  "Sex" : "M"
}, {
  "firstName" : "E",
  "lastName" : "F",
  "age" : 21,
  "Sex" : "M"
}, {
  "firstName" : "G",
  "lastName" : "H",
  "age" : 55,
  "Sex" : "M"
} ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM