简体   繁体   English

如何反序列化JSON数组?

[英]How to Deserialize JSON Array?

I'm using Jackson within CXF to serialize/deserialize data. 我在CXF中使用Jackson来序列化/反序列化数据。 Unfortunately, I am having difficulty configuring CXF/Jackson to deserialize a JSON array. 不幸的是,我很难配置CXF / Jackson来反序列化JSON数组。 I'd appreciate help in resolving the issue. 我很感激帮助解决这个问题。

Up to this point most of the json data has been in object format, ie 到目前为止,大多数json数据都是对象格式,即

{ "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. 我将@JsonDeserialize如下添加到GroupsDto集合属性中,但它不起作用。 I continue to get: "Can not deserialize instance of GroupsDto out of START_ARRAY token" 我继续得到:“无法将GroupsDto的实例反序列化为START_ARRAY标记”

public class GroupsDto {

       private Collection<GroupDto> groups;

       /**
        * @return the groups
        */
       @XmlElement(name="group")
       @JsonDeserialize(contentAs=GroupDto.class)
       public Collection<GroupDto> getGroups() {
               return groups;
       }
...
}

If json data is of the form: 如果json数据的形式如下:

[ {...}, {...}, {...} ]

You got to use add another class say 'wrapper': 你必须使用添加另一个类说'wrapper':

@JsonIgnoreProperties(ignoreUnknown = true)
public class ListDto extends ArrayList<GroupDto> {

    public ListDto() {
    }
}

And use this class while deserailizing. 在取消选择时使用这个类。 This approach worked for me. 这种方法对我有用。

You just need to specify the @JsonDeserialize(contentAs=GroupDto.class) in your setter . 您只需要在setter中指定@JsonDeserialize(contentAs=GroupDto.class) Serialization is always on get desserialization is always on set , or if you prefer you can specify both on the field. 序列化始终是GET desserialization总是在 ,或者如果你喜欢,你可以在球场上同时指定。

Documentation for Serialize and Deserialize 序列化 序列化的文档

Code sample: 代码示例:

import java.io.IOException;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonDeserialize;

public class JacksonDeserialize {

    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));
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM