[英]Deserializing JSON using Jackson 2
I have a json 我有一个json
[
{
"host": {
"name": "anotherfullhost",
"id": 55602819,
"operatingsystem_id": 1073012828,
"hostgroup_id": null
}
},
{
"host": {
"name": "dhcp.mydomain.net",
"id": 219245707,
"operatingsystem_id": 1073012828,
"hostgroup_id": null
}
},
{
"host": {
"name": "my5name.mydomain.net",
"id": 980190962,
"operatingsystem_id": 1073012828,
"hostgroup_id": null
}
}
]
I would like to construct a Collection by deserializing the above json. 我想通过反序列化上面的json来构造一个Collection。 What jackson annotations should I add to the below Host class
我应该在以下Host类中添加哪些jackson批注
public class Host {
@JsonProperty("id")
private Long id;
@JsonProperty("name")
private String name;
@JsonProperty("operatingsystem_id")
private Long operatingSystemId;
@JsonProperty("hostgroup_id")
private Long hostGroupId;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getOperatingSystemId() {
return operatingSystemId;
}
public void setOperatingSystemId(Long operatingSystemId) {
this.operatingSystemId = operatingSystemId;
}
public Long getHostGroupId() {
return hostGroupId;
}
public void setHostGroupId(Long hostGroupId) {
this.hostGroupId = hostGroupId;
}
@Override
public String toString() {
return "Host{" +
"name='" + name + '\'' +
'}';
}
}
Any suggestions? 有什么建议么?
Note - I am using jackson 2.x API. 注-我正在使用杰克逊2.x API。
Thanks. 谢谢。
Update Adding a wrapper object does the trick. 更新添加一个包装对象就可以了。
public class HostWrapper {
@JsonProperty("host")
private Host host;
public Host getHost() {
return host;
}
public void setHost(Host host) {
this.host = host;
}
@Override
public String toString() {
return host.toString();
}
}
and the below code to deserialize
ObjectMapper mapper = new ObjectMapper();
HostWrapper[] host = mapper.readValue(jsonString, HostWrapper[].class);
Please see this post - This should be the same issue than yours, even if a different API is used: 请查看这篇文章-即使使用了不同的API,这也应该是与您相同的问题:
JsonMappingException: Current token not START_OBJECT (needed to unwrap root name 'Transaction[]'), but START_ARRAY JsonMappingException:当前令牌不是START_OBJECT(需要解包根名'Transaction []'),而是START_ARRAY
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.