简体   繁体   English

Json 数组通过 jackson 在 java 中列出

[英]Json array to list to list in java via jackson

public class Operation {
    private int id;
    private String operator;
    private int room;
    private long operationDateMillis;
    private String accessory;
    private List<String> statuses; 
    
    //setters getters
}

I have this class and I need to store from a JSON an array of Operation in another class.我有这个 class,我需要从 JSON 存储另一个 class 中的Operation数组。

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

Here's my JSON file:这是我的 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"
      ]
    }
  ]
}

I already developed a JSON parsing class and Manage to get array nodes from the JSON file.我已经开发了一个 JSON 解析 class 并管理从 JSON 文件中获取阵列节点。

You could use Jackson ObjectMapper to map JSON to your class.您可以使用 Jackson ObjectMapper 到 map JSON 到您的 ZA2F2ED4F8EBC2CBB61A29DC4。 Example:例子:

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: """ -> Text block is Java-13 feature. PS: """ -> 文本块是 Java-13 的特性。

You can have lot of serialization and deserialization configs in ObjectMapper.您可以在 ObjectMapper 中有很多序列化和反序列化配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM