如何反序列化JSON数组? [英] How to Deserialize JSON Array?
问题描述
我在CXF中使用Jackson来序列化/反序列化数据。不幸的是,我很难配置CXF / Jackson来反序列化JSON数组。我很感激帮助解决这个问题。
I'm using Jackson within CXF to serialize/deserialize data. Unfortunately, I am having difficulty configuring CXF/Jackson to deserialize a JSON array. I'd appreciate help in resolving the issue.
到目前为止,大多数json数据都是对象格式,即
Up to this point most of the json data has been in object format, i.e.
{objectCollection:[{...},{...},{...} ...]}
但是,有问题的json数据的格式如下:
However, the json data in question is of the form:
[{... },{...},{...}]
Web服务端点需要GroupsDto对象(请参阅下文)
有一个属性 - 一组组,通过JSON数组传输
。
The web service endpoint expects a "GroupsDto" object (see following) that has a single property -- a collection of groups, which is transmitted via the JSON array.
@PATH(...)
public Response createGroups(GroupsDto groups) {
...
}
我将@JsonDeserialize如下添加到GroupsDto集合属性中,但它不起作用。我继续说:无法将GroupsDto的实例反序列化为START_ARRAY令牌
I added @JsonDeserialize as follows to the GroupsDto collection property, but it does NOT work. I continue to get: "Can not deserialize instance of GroupsDto out of START_ARRAY token"
public class GroupsDto {
private Collection<GroupDto> groups;
/**
* @return the groups
*/
@XmlElement(name="group")
@JsonDeserialize(contentAs=GroupDto.class)
public Collection<GroupDto> getGroups() {
return groups;
}
...
}
推荐答案
如果json数据的格式如下:
If json data is of the form:
[ {...}, {...}, {...} ]
你必须使用添加另一个类说'wrapper':
You got to use add another class say 'wrapper':
@JsonIgnoreProperties(ignoreUnknown = true)
public class ListDto extends ArrayList<GroupDto> {
public ListDto() {
}
}
在取消选择时使用此课程。这种方法对我有用。
And use this class while deserailizing. This approach worked for me.
这篇关于如何反序列化JSON数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!