【问题标题】:Json array to list to list in java via jacksonJson数组通过jackson列出以在java中列出
【发布时间】:2021-05-03 13:49:41
【问题描述】:
public class Operation {
    private int id;
    private String operator;
    private int room;
    private long operationDateMillis;
    private String accessory;
    private List<String> statuses; 
    
    //setters getters
}

我有这个类,我需要从 JSON 中存储一个 Operation 数组到另一个类中。

public class OperationRoom {
    public static List<Operation> operations;
}

这是我的 JSON 文件:

{
  "operations": [
    {
      "id": 1,
      "operator": "william",
      "room": 1,
      "operationDate": "1620042283092",
      "accessory": "Security",
      "statuses": [
        "turned on",
        "temp 10",
        "fan off"
      ]
    },
    {
      "id": 2,
      "operator": "wilo",
      "room": 5,
      "operationDate": "1620042483092",
      "accessory": "Security",
      "statuses": [
        "turned off",
        "temp 15",
        "fan on"
      ]
    }
  ]
}

我已经开发了一个 JSON 解析类和管理从 JSON 文件中获取数组节点。

【问题讨论】:

  • 杰克逊可以为你做这一切。根据环境的不同,方法会略有不同。例如 Spring、注解等。

标签: java arrays json parsing jackson


【解决方案1】:

您可以使用 Jackson ObjectMapper 将 JSON 映射到您的类。示例:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test {

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        OperationRoom operationRoom = objectMapper.readValue(getJson(), OperationRoom.class);
        System.out.println(operationRoom);
    }

    static String getJson() {
        return """
                    {
                      "operations": [
                        {
                          "id": 1,
                          "operator": "william",
                          "room": 1,
                          "operationDate": "1620042283092",
                          "accessory": "Security",
                          "statuses": [
                            "turned on",
                            "temp 10",
                            "fan off"
                          ]
                        },
                        {
                          "id": 2,
                          "operator": "wilo",
                          "room": 5,
                          "operationDate": "1620042483092",
                          "accessory": "Security",
                          "statuses": [
                            "turned off",
                            "temp 15",
                            "fan on"
                          ]
                        }
                      ]
                    }
            """;
    }
}

PS: """ -> 文本块是 Java-13 的特性。

您可以在 ObjectMapper 中进行大量的序列化和反序列化配置。

【讨论】:

    猜你喜欢
    • 2018-07-14
    • 2019-05-24
    • 1970-01-01
    • 2020-07-25
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多