java 如何使用 Jackson 或任何其他 api 反序列化一组对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29807818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to deserialize an array of objects using Hymanson or any other api?
提问by user3818373
I have a model defined to which i need to map the jsonArray.The model is as follows:
我定义了一个模型,我需要将 jsonArray 映射到该模型。模型如下:
@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
}
The response is JSONArray and this is what i have to map to the Container Class which is as follows:
响应是 JSONArray,这是我必须映射到容器类的内容,如下所示:
[
{
"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());
}
Or
或者
List<Container> containerList =
mapper.readValue(containerResponse, new TypeReference<List<MyClass>>(){});
I followed Hymanson Deserializeand 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 ?
我跟着Hymanson Deserialize并尝试了帖子中提到的所有解决方案。它无法映射到模型。所有字段都是空的,它读取值。没有值映射到 Container 类的属性。模型中缺少任何东西还是我需要编写客户逻辑来反序列化 json ?
回答by Raniz
First off, I'm assuming that
首先,我假设
"Labels": {
},
should actually be
实际上应该是
"Labels": [
],
since you've defined it to be a list of Strings and not an object of it's own.
因为您已将其定义为字符串列表而不是它自己的对象。
This code works for me:
这段代码对我有用:
Port.java:
端口.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
Container Id : d70f439231d99889c1a8e96148f13a77cb9b83ecf8c9d4c691ddefa40286c04c
2
容器 ID:c00afc1ae5787282fd92b3dde748d203a83308d18aaa566741bef7624798af10
容器 ID:d70f439231d99889c1a8e6c8c8c8c8c8c8c8c8c8c8c8c81961dc8aaa566741
回答by mkobit
I'm surprised you are not getting an exception when trying to deserialize the object. When I run your code locally I get a
我很惊讶您在尝试反序列化对象时没有收到异常。当我在本地运行你的代码时,我得到一个
Exception in thread "main" com.fasterxml.Hymanson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
线程“main”com.fasterxml.Hymanson.databind.JsonMappingException 中的异常:无法从 START_OBJECT 令牌反序列化 java.util.ArrayList 的实例
The reason looking like you are trying to serialize into an Array when it is a JSON object.
当它是一个 JSON 对象时,看起来你试图序列化成一个数组的原因。
"Labels":{
},
That is a JSON object, so you should change your type to reflect what it is, or change the serialization of that JSON property to be an array. If you just want to change the type in your Container
class, you should use a Map
:
这是一个 JSON 对象,因此您应该更改您的类型以反映它是什么,或者将该 JSON 属性的序列化更改为一个数组。如果您只想更改类中的Container
类型,则应使用Map
:
@JsonProperty("Labels")
private Map<String, Object> Labels;