【问题标题】:JAVA Jackson Parsing JSON Response that Contains List/ArrayJAVA Jackson 解析包含列表/数组的 JSON 响应
【发布时间】:2015-04-25 02:25:35
【问题描述】:

我正在尝试反序列化 JSON 字符串,但我一直遇到 JSON 映射异常。我一直在网上搜索,但运气不佳。

我尝试反序列化的 JSON 响应如下所示:

{
   "comments": [{
         "id": "fa6491aeb",
         "user": {
            "userId": "e4dddf5e1",
            "username": "UserX",
            "name": "UserX",
            "profilePhotoUri": ""
         },
         "message": "8: 23 - UserX",
         "timestamp": 1429844781919
      },{
         "id": "ed3e71",
         "user": {
            "userId": "20b8f1",
            "username": "UserW",
            "name": "UserW",
            "profilePhotoUri": ""
         },
         "message": "8: 22 - UserW",
         "timestamp": 1429844780250
      },
      //... more items
   ],
   "canCallerComment": true
}

这是我得到的错误的删减版本:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.test.android.CommentsResponse out of START_ARRAY token
    at [Source: [{"user":{"userId":"fa6491aeb", ..........]; line: 1, column: 1]
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:835)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:831)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1220)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:165)
    ...

我尝试按照this post 中的说明包装我的回复,但我仍然遇到同样的错误。从调用堆栈跟踪来看,它似乎与 List 有关。即,我需要传递一个列表对象。这 objectMapper.readValue(json, CommentResponse.class) 还不够吗?

我的 JAVA 类定义如下:

public class Comment {
    private String id;
    private User user;
    private String message;
    private long timestamp;
    // getter/setters
}

public class CommentResponse {
    List<Comment> comments;
    boolean canCallerComment = false;
    // getter/setters
}

如果有帮助,我使用的是 Jackson 2.5.3 版;目标平台是 Android。

编辑: 下面的火花解决方案是正确的。我在尝试从错误的 Web 服务解析 JSON 时出现拼写错误。

【问题讨论】:

    标签: java android json jackson deserialization


    【解决方案1】:

    您的 JSON 格式不正确。从错误信息中可以看出:

    [Source: [{"user":{"userId":"fa6491aeb", ..........]; 
    

    解析器似乎遇到了一个数组而不是一个对象。

    【讨论】:

    • 我确实尝试过按照您的建议进行初始化。然而那失败了。我刚刚用你的代码试了一下,同样的错误仍然存​​在。此外,我尝试了您链接到的工具。它还会生成您建议的代码。
    • 非常感谢!!!你说的对。我的回答不正确。我收到了另一个 Web 服务的响应。我的错字。谢谢!!!!
    【解决方案2】:

    您的代码接缝正确。这是我运行和工作的代码:

    test.json

    {
        "comments": [
        {
            "id": "fa6491aeb",
            "user": {
                "userId": "e4dddf5e1",
                "username": "UserX",
                "name": "UserX",
                "profilePhotoUri": ""
            },
            "message": "8: 23 - UserX",
            "timestamp": 1429844781919
        },
        {
            "id": "ed3e71",
            "user": {
                "userId": "20b8f1",
                "username": "UserW",
                "name": "UserW",
                "profilePhotoUri": ""
            },
            "message": "8: 22 - UserW",
            "timestamp": 1429844780250
        }],
        "canCallerComment": true
    }
    

    Java

    public static void main(String[] args) {
    
        ObjectMapper objectMapper = new ObjectMapper();
    
        CommentResponse commentResponse;
        try {
            commentResponse = objectMapper.readValue(
                    new File("P:\\projects\\tests\\json-test\\src\\main\\resources\\test.json"),
                    CommentResponse.class);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    
        commentResponse.getComments();
    
    }
    
    public static class User {
        private String userId;
        private String username;
        private String name;
        private String profilePhotoUri;
    
        public String getUserId() {
            return userId;
        }
    
        public void setUserId(String userId) {
            this.userId = userId;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getProfilePhotoUri() {
            return profilePhotoUri;
        }
    
        public void setProfilePhotoUri(String profilePhotoUri) {
            this.profilePhotoUri = profilePhotoUri;
        }
    }
    
    public static class Comment {
        private String id;
        private User user;
        private String message;
        private long timestamp;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public long getTimestamp() {
            return timestamp;
        }
    
        public void setTimestamp(long timestamp) {
            this.timestamp = timestamp;
        }
    }
    
    public static class CommentResponse {
        List<Comment> comments;
        boolean canCallerComment = false;
    
        public List<Comment> getComments() {
            return comments;
        }
    
        public void setComments(List<Comment> comments) {
            this.comments = comments;
        }
    
        public boolean isCanCallerComment() {
            return canCallerComment;
        }
    
        public void setCanCallerComment(boolean canCallerComment) {
            this.canCallerComment = canCallerComment;
        }
    }
    

    【讨论】:

    • 我的代码中实际上有一个类型。我正在调用错误的 Web 服务。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    相关资源
    最近更新 更多