嵌套的JSONObject反序列化为JSONObject [英] Nested JSONObject Deserialize to JSONObject

查看:300
本文介绍了嵌套的JSONObject反序列化为JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在努力反序列化嵌套的JSONObject,但不想为每个嵌套的对象创建一个类。我正在尝试使用嵌套的JSONObject,并将其放入JSONObject。

So I'm working on deserializing a nested JSONObject, but don't want to create a class for each nested object. I was trying to take on of the nested JSONObjects and put it in a 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 state = {
terminated:{
exitCode:1,
reason: Error,
startedAt: 2019-03- 20T15:40:08Z,
finishedAt: 2019-03-20T15:40:50Z,
containerID: docker://
}
}

我可以将其转换为通用对象,但格式不再是JSON:

I can cast it into a generic object, but the format isn't JSON anymore:

@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 而不是 JSONObject ,它可能来自 org.json

  • DeserializationFeature.UNWRAP_ROOT_VALUE, enable this feature to unwrap JSON object.
  • Add JsonRootName annotation to your POJO class because class name does not match to property containerStatuses.
  • Use JsonNode which comes from Jackson library instead of JSONObject which comes probably from org.json.

改进的示例如下所示:

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://"}}

这篇关于嵌套的JSONObject反序列化为JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆