1

I'm trying to deserialize a JSON string but I keep running into JSON Mapping Exception. I've been scouring the internet but have had little luck.

My JSON response that I'm trying to deserialize looks like the following:

{
   "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
}

Here is an abridged version of the error I get:

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)
    ...

I tried wrapping my response as mentioned in this post, but I still get the same error. From the call stack-trace, it seems likes it has something to do with List<Comment>. I.e, I need to pass a list object. Is this objectMapper.readValue(json, CommentResponse.class) not sufficient?

My JAVA classes are defined as follows:

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
}

If it helps, I'm using Jackson version 2.5.3; and the targeted platform is Android.

Edit: Sparks solution below is correct. I had a typo where I was trying to parse JSON from the wrong web service.

2 Answers 2

3

Your JSON is malformed. As you can see from the error message:

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

it seems the parser has encountered an array instead of an object.

Sign up to request clarification or add additional context in comments.

2 Comments

I actually did try initializing like you suggested. However that failed. I just tried it with your code and the same error persists. Additionally, I tried the tool you linked to. It also generates the code you suggested.
Thank you so much!!! You are right. My response was not correct. I was getting the response for another web service. Typo on my end. Thank you!!!!
2

Your code seams correct. Here is the code I ran and worked:

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

1 Comment

I actually had a type in my code. I was making a call to the wrong web service. Thank you for your response.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.