java 如何反序列化 JSON 数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11106379/
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 JSON Array?
提问by Ari
I'm using Hymanson within CXF to serialize/deserialize data. Unfortunately, I am having difficulty configuring CXF/Hymanson to deserialize a JSON array. I'd appreciate help in resolving the issue.
我在 CXF 中使用 Hymanson 来序列化/反序列化数据。不幸的是,我在配置 CXF/Hymanson 以反序列化 JSON 数组时遇到了困难。我很感激帮助解决这个问题。
Up to this point most of the json data has been in object format, i.e.
到目前为止,大部分json数据都是对象格式,即
{ "objectCollection": [ {...}, {...}, {...}... ] }
{ "objectCollection": [ {...}, {...}, {...}... ] }
However, the json data in question is of the form:
但是,有问题的 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.
Web 服务端点需要一个“GroupsDto”对象(见下文),它具有单个属性——一组组,通过 JSON 数组传输。
@PATH(...)
public Response createGroups(GroupsDto groups) {
...
}
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"
我将 @JsonDeserialize 添加到 GroupsDto 集合属性中,如下所示,但它不起作用。我继续得到:“无法从 START_ARRAY 令牌中反序列化 GroupsD 的实例”
public class GroupsDto {
private Collection<GroupDto> groups;
/**
* @return the groups
*/
@XmlElement(name="group")
@JsonDeserialize(contentAs=GroupDto.class)
public Collection<GroupDto> getGroups() {
return groups;
}
...
}
回答by Snehal Masne
If json data is of the form:
如果 json 数据是以下形式:
[ {...}, {...}, {...} ]
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.
并在脱轨时使用这个类。这种方法对我有用。
回答by Francisco Spaeth
You just need to specify the @JsonDeserialize(contentAs=GroupDto.class)
in your setter. Serialization is always on getdesserialization is always on set, or if you prefer you can specify both on the field.
你只需要@JsonDeserialize(contentAs=GroupDto.class)
在你的setter 中指定。序列化始终在get反序列化始终在set 上,或者如果您愿意,可以在字段中同时指定两者。
Documentation for Serializeand Deserialize
Code sample:
代码示例:
import java.io.IOException;
import java.util.List;
import org.codehaus.Hymanson.JsonGenerationException;
import org.codehaus.Hymanson.map.JsonMappingException;
import org.codehaus.Hymanson.map.ObjectMapper;
import org.codehaus.Hymanson.map.annotate.JsonDeserialize;
public class HymansonDeserialize {
public static class ModelClass {
private String name;
public ModelClass() {
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public ModelClass(final String name) {
super();
this.name = name;
}
@Override
public String toString() {
return "ModelClass [name=" + name + "]";
}
}
public static class ListModelClass {
private List<ModelClass> list;
@JsonDeserialize(contentAs = ModelClass.class)
public void setList(final List<ModelClass> list) {
this.list = list;
}
@Override
public String toString() {
return "ListModelClass [list=" + list + "]";
}
}
public static void main(final String[] args) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.readValue("{\"list\":[{\"name\":\"name1\"},{\"name\":\"name2\"}]}",
ListModelClass.class));
}
}