用杰克逊(Java)从Moodle Web服务反序列化json

时间:2016-10-27 10:09:56

标签: java json jackson

我需要解析从调用Moodle REST api获得的JSON对象。我得到的JSON响应是:

{"users":[{"id":2,"username":"admin","firstname":"Amministratore","lastname":"Utente","fullname":"Amministratore Utente","email":"email@address.com","department":"","firstaccess":1475831148,"lastaccess":1477493148,"description":"","descriptionformat":1,"profileimageurlsmall":"http:\/\/vmoodle.local\/theme\/image.php\/clean\/core\/1475846316\/u\/f2","profileimageurl":"http:\/\/vmoodle.local\/theme\/image.php\/clean\/core\/1475846316\/u\/f1","preferences":[{"name":"auth_manual_passwordupdatetime","value":"1475831184"},{"name":"email_bounce_count","value":"1"},{"name":"email_send_count","value":"1"},{"name":"_lastloaded","value":1477561370}]}],"warnings":[]}

我想把这个json映射到POJO,所以我写了两个类:

public class MoodleUser {

        public MoodleUser() {
        }

        private int id;
        private String username;

        /**
         * @return the id
         */
        public int getId() {
            return id;
        }

        /**
         * @param id the id to set
         */
        public void setId(int id) {
            this.id = id;
        }

        /**
         * @return the username
         */
        public String getUsername() {
            return username;
        }

        /**
         * @param username the username to set
         */
        public void setUsername(String username) {
            this.username = username;
        }

    }

    public class MoodleUsers {

        public MoodleUsers() {

        }

        private List<MoodleUser> users;

        /**
         * @return the users
         */
        public List<MoodleUser> getUsers() {
            return users;
        }

        /**
         * @param users the users to set
         */
        public void setUsers(List<MoodleUser> users) {
            this.users = users;
        }

    }

然后我打电话给:

MoodleUsers response = oMapper.readValue(result, MoodleUsers.class);

我得到的是异常报告以下错误:

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of mypackage.lms.MoodleClientRest$MoodleUsers: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
 at [Source: {"users":[{"id":12,"username":"userok","firstname":"Nome test","lastname":"Cognome test","fullname":"Nome test Cognome test","email":"email@test.it","department":"","idnumber":"cftest","firstaccess":0,"lastaccess":0,"description":"","descriptionformat":1,"country":"IT","profileimageurlsmall":"http:\/\/vmoodle.local\/theme\/image.php\/clean\/core\/1475846316\/u\/f2","profileimageurl":"http:\/\/vmoodle.local\/theme\/image.php\/clean\/core\/1475846316\/u\/f1"}],"warnings":[]}
; line: 1, column: 2]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:261)
    at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1456)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1012)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1203)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:314)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2833)

我已经在我的pojo中拥有默认构造函数,因此我无法理解问题所在的位置。有什么建议? 谢谢。

1 个答案:

答案 0 :(得分:0)

摘要中的类的名称:mypackage.lms.MoodleClientRest$MoodleUsers表明MoodleUsersMoodleClientRest的内部类,在这种情况下, 是公开的static,以便jackson成功实例化对象。