簡體   English   中英

嵌套的JSONObject反序列化為JSONObject

[英]Nested JSONObject Deserialize to JSONObject

因此,我正在反序列化嵌套的JSONObject,但不想為每個嵌套的對象創建一個類。 我試圖使用嵌套的JSONObjects並將其放入JSONObject中。

public class ContainerStatus {

@JsonProperty("name")
private String name;
@JsonProperty("state")
private JSONObject state;
@JsonProperty("lastState")
private JSONObject  lastState;
@JsonProperty("ready")
private Boolean ready;
@JsonProperty("restartCount")
private Integer restartCount;
@JsonProperty("image")
private String image;
@JsonProperty("imageID")
private String imageID;
@JsonProperty("containerID")
private String containerID;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

使用這個來反序列化:

 { "containerStatuses": 
        {
            "name": "connect",
            "state": {
                        "terminated": {
                            "exitCode": 1,
                            "reason": "Error",
                            "startedAt": "2019-03-20T15:40:08Z",
                            "finishedAt": "2019-03-20T15:40:50Z",
                            "containerID": "docker://"
                        }
                    },
            "lastState": {},
            "ready": true,
            "restartCount": 0,
            "image": "image",
            "imageID": "docker-pullable://",
            "containerID": "docker://"
        }}

由於狀態JSONObject,我無法識別字段“終止”。

我想要一個:

JsonObject state = { "terminated": { "exitCode": 1, "reason": "Error", "startedAt": "2019-03-20T15:40:08Z", "finishedAt": "2019-03-20T15:40:50Z", "containerID": "docker://" } }

我可以將其轉換為通用對象,但格式不再是JSON:

@JsonProperty("state")
private Object state;

Gives me this format:
{running={startedAt=2019-03-20T14:53:53Z}}

您需要改進一下示例:

  • DeserializationFeature.UNWRAP_ROOT_VALUE ,啟用此功能可解包裝JSON對象。
  • JsonRootName批注添加到POJO類中,因為類名與屬性containerStatuses不匹配。
  • 使用來自Jackson庫的JsonNode代替可能來自org.jsonJSONObject

改進的示例如下所示:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        String json = "{...}";
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);

        ContainerStatus cs = mapper.readValue(json, ContainerStatus.class);
        System.out.println(cs.getState());
    }
}

@JsonRootName("containerStatuses")
class ContainerStatus {

    @JsonProperty("name")
    private String name;
    @JsonProperty("state")
    private JsonNode state;
    @JsonProperty("lastState")
    private JsonNode lastState;
    @JsonProperty("ready")
    private Boolean ready;
    @JsonProperty("restartCount")
    private Integer restartCount;
    @JsonProperty("image")
    private String image;
    @JsonProperty("imageID")
    private String imageID;
    @JsonProperty("containerID")
    private String containerID;

    // getters, setters, toString
}

上面的代碼打印:

{"terminated":{"exitCode":1,"reason":"Error","startedAt":"2019-03-20T15:40:08Z","finishedAt":"2019-03-20T15:40:50Z","containerID":"docker://"}}

暫無
暫無

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

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