如何使用Jackson或任何其他api反序列化对象数组? [英] How to deserialize an array of objects using Jackson or any other api?

查看:103
本文介绍了如何使用Jackson或任何其他api反序列化对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,我需要映射jsonArray。模型如下:

I have a model defined to which i need to map the jsonArray.The model is as follows:

@JsonPropertyOrder({
    "Command",
    "Created",
    "Id",
    "Image",
    "Labels",
    "Names",
    "Ports",
    "Status"
})
public class Container {

    @JsonProperty("Command")
    private String Command;

    @JsonProperty("Created")
    private long Created;

    @JsonProperty("Id")
    private String Id;

    @JsonProperty("Image")
    private String Image;

    @JsonProperty("Labels")
    private List<String> Labels;

    @JsonProperty("Names")
    private List<String> Names = new ArrayList<String>();

    @JsonProperty("Ports")
    private List<Port> Ports = new ArrayList<Port>();

    @JsonProperty("Status")
    private String Status;

    //Getters and Setters
}

回复是JSONArray,这是我必须映射到容器类,如下所示:

The response is JSONArray and this is what i have to map to the Container Class which is as follows:

[  
  {  
    "Command":"/usr/sbin/sshd -D",
    "Created":1429686533,
    "Id":"c00afc1ae5787282fd92b3dde748d203a83308d18aaa566741bef7624798af10",
    "Image":"jay8798:latest",
    "Labels":{  

    },
    "Names":[  
      "/jay8798"
    ],
    "Ports":[  
      {  
        "IP":"0.0.0.0",
        "PrivatePort":22,
        "PublicPort":32845,
        "Type":"tcp"
      },
      {  
        "IP":"0.0.0.0",
        "PrivatePort":3306,
        "PublicPort":32846,
        "Type":"tcp"
      }
    ],
    "Status":"Up 12 hours"
  },
  {  
    "Command":"/usr/sbin/sshd -D",
    "Created":1429686039,
    "Id":"d70f439231d99889c1a8e96148f13a77cb9b83ecf8c9d4c691ddefa40286c04c",
    "Image":"jay898:latest",
    "Labels":{  

    },
    "Names":[  
      "/jay898"
    ],
    "Ports":[  
      {  
        "IP":"0.0.0.0",
        "PrivatePort":22,
        "PublicPort":32841,
        "Type":"tcp"
      },
      {  
        "IP":"0.0.0.0",
        "PrivatePort":3306,
        "PublicPort":32842,
        "Type":"tcp"
      }
    ],
    "Status":"Up 12 hours"
  }
]

这是我尝试的但似乎没有任何效果:

This is what I have tried but nothing seems to be working:

Container[] containerList = mapper.readValue(containerResponse, Container[].class);
int totalContainers = containerList.length;

//This will return correct length of the container.
System.out.println("This is the retrived size : " + containerList.length); 

for(Container cont : containerList) {
    // this statement will print 'null'.There is no exception thrown at this point.
    System.out.println("Container Id : "+cont.getId());
}

List<Container> containerList = 
          mapper.readValue(containerResponse, new TypeReference<List<MyClass>>(){});

我跟着杰克逊反序列化并尝试了帖子中提到的所有解决方案。
它无法映射到模型。所有字段都为null,它读取值。没有值映射到Container类的属性。模型中缺少任何东西或者我是否需要编写客户逻辑来反序列化json?

I followed Jackson Deserialize and tried all the solutions mentioned in the post. It is not able to map to the model.All the fields are null one it reads the values. No value is mapped to the attribute of Container class.Any thing missing in the model or do i need to write a customer logic to deserialize the json ?

推荐答案

首先,我假设

"Labels": {
},

实际应该是

"Labels": [
],

因为你定义了它是一个字符串列表而不是它自己的对象。

since you've defined it to be a list of Strings and not an object of it's own.

此代码适用于我:

Port.java:

Port.java:

public class Port {
    @JsonProperty("IP")
    private String ip;

    @JsonProperty("PrivatePort")
    private int privatePort;

    @JsonProperty("PublicPort")
    private int publicPort;

    @JsonProperty("Type")
    private String type;

    // Getters and setters
}

main() :

String json = ... // Your JSON with the above correction
ObjectMapper objectMapper = new ObjectMapper();
List<Container> containers = objectMapper.readValue(json, new TypeReference<List<Container>>() {});
System.out.println(containers.size());
for(Container container : containers) {
    System.out.println("Container Id : " + container.getId());
}




2

Container Id:c00afc1ae5787282fd92b3dde748d203a83308d18aaa566741bef7624798af10

容器ID:d70f439231d99889c1a8e96148f13a77cb9b83ecf8c9d4c691ddefa40286c04c

2
Container Id : c00afc1ae5787282fd92b3dde748d203a83308d18aaa566741bef7624798af10
Container Id : d70f439231d99889c1a8e96148f13a77cb9b83ecf8c9d4c691ddefa40286c04c

这篇关于如何使用Jackson或任何其他api反序列化对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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