如何使用 Jackson 或任何其他 api 反序列化对象数组?
How to deserialize an array of objects using Jackson or any other api?
我定义了一个模型,我需要将其映射到 jsonArray.The 模型如下:
@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,这是我必须映射到容器 Class 的内容,如下所示:
[
{
"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"
}
]
这是我尝试过的方法,但似乎没有任何效果:
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>>(){});
我按照 Jackson Deserialize 并尝试了 post 中提到的所有解决方案。
它无法映射到 model.All 字段为空,它读取值。没有值映射到模型中缺少的容器 class.Any 的属性,或者我是否需要编写客户逻辑来反序列化 json?
首先,我假设
"Labels": {
},
其实应该是
"Labels": [
],
因为您已将其定义为字符串列表而不是它自己的对象。
这段代码对我有用:
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
}
主要():
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
Container Id : d70f439231d99889c1a8e96148f13a77cb9b83ecf8c9d4c691ddefa40286c04c
我很惊讶您在尝试反序列化对象时没有遇到异常。当我在本地 运行 你的代码时,我得到
Exception in thread "main"
com.fasterxml.jackson.databind.JsonMappingException: Can not
deserialize instance of java.util.ArrayList out of START_OBJECT token
当它是一个 JSON 对象时,看起来你试图序列化到数组中的原因。
"Labels":{
},
这是一个 JSON 对象,因此您应该更改类型以反映它是什么,或者将 JSON 属性 的序列化更改为数组。如果您只想更改 Container
class 中的类型,您应该使用 Map
:
@JsonProperty("Labels")
private Map<String, Object> Labels;
我定义了一个模型,我需要将其映射到 jsonArray.The 模型如下:
@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,这是我必须映射到容器 Class 的内容,如下所示:
[
{
"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"
}
]
这是我尝试过的方法,但似乎没有任何效果:
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>>(){});
我按照 Jackson Deserialize 并尝试了 post 中提到的所有解决方案。 它无法映射到 model.All 字段为空,它读取值。没有值映射到模型中缺少的容器 class.Any 的属性,或者我是否需要编写客户逻辑来反序列化 json?
首先,我假设
"Labels": {
},
其实应该是
"Labels": [
],
因为您已将其定义为字符串列表而不是它自己的对象。
这段代码对我有用:
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
}
主要():
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
Container Id : d70f439231d99889c1a8e96148f13a77cb9b83ecf8c9d4c691ddefa40286c04c
我很惊讶您在尝试反序列化对象时没有遇到异常。当我在本地 运行 你的代码时,我得到
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
当它是一个 JSON 对象时,看起来你试图序列化到数组中的原因。
"Labels":{
},
这是一个 JSON 对象,因此您应该更改类型以反映它是什么,或者将 JSON 属性 的序列化更改为数组。如果您只想更改 Container
class 中的类型,您应该使用 Map
:
@JsonProperty("Labels")
private Map<String, Object> Labels;