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.