当前位置: 首页 > 知识库问答 >
问题:

反序列化json从Moodle Web服务与杰克逊(java)

杜嘉木
2023-03-14

我需要解析从调用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个答案

彭衡
2023-03-14

expetion:mypackage中类的名称。lms。MoodleClientRest$MoodleUsers表明MoodleUsersMoodleClientRest的一个内部类,在这种情况下,它必须是公共静态的,以便jackson成功实例化对象

 类似资料:
  • 我正在调用一个返回JSON的endpoint,它看起来像这样(在Postman中): 此请求返回的Content-Type头是(与通常的 类来自外部库(编写这个endpoint的人)。无论如何,当我试图通过< code > rest template . exchange()调用这个endpoint时,Jackson都无法将这个JSON反序列化为一个有效的< code>Result类。我正在这样做

  • 有没有办法让Jackson序列化某个流对象(并在之后关闭)?这样地: 使现代化 澄清:我想流式传输内容,而不仅仅是将其序列化到单个String对象。

  • 问题内容: 这是我的Java代码,用于反序列化,我正在尝试将json字符串转换为java对象。为此,我使用了以下代码: 和我的product.java类 我收到以下错误。 帮我解决这个问题 问题答案: 似乎您正在尝试从JSON读取一个实际描述数组的对象。Java对象使用花括号映射到JSON对象,但是您的JSON实际上以方括号指定一个数组开始。 您实际拥有的是一个。为了描述泛型类型,由于Java的类

  • 问题内容: 我有一个这样的模型: 例如,我从远程获取以下JSON: 当我反序列化此JSON时,and 变量将获得正确的值。但是,我不想解释变量的内容。相反,我希望它是以下字符串: 之后,我将自己解释。我如何获得这个值? 问题答案: Jackson问题596是为原始问题中描述的所需功能而创建的。如果要实施,请投票。 当前可用的解决方案是实现自定义反序列化处理。

  • 我明白它无法从嵌套JSON反序列化字符串。我解决这个问题的一个方法是通过编写自定义映射器 但是只要pojo中有变化,这就需要修改映射器,添加/删除字段。Mixin工作时没有任何变化,我该如何更新我的Mixin来处理objectId呢?

  • 我想用scala中的jackson反序列化json json_结构: {“type”:“struct”,“fields”:[{“name”:“code_role”,“type”:“string”,“nullable”:true,“metadata”:{“HIVE_type_string”:“string”},{“name”:“libelle_role”,“type”:“string”,“nulla