I have a json like this:
{
"games": [
{
"id": "mhhlhlmlezgwniokgawxloi7mi",
"from": "425364_456@localhost",
"to": "788295_456@localhost",
"token": "xqastwxo5zghlgjcapmq5tirae",
"desc": "6CeF9/YEFAiUPgLaohbWt9pC7rt9PJlKE6TG6NkA4hE=",
"timestamp": 1412806372232
},
{
"id": "62jzlm64zjghna723grfyb6y64",
"from": "425364_456@localhost",
"to": "788295_456@localhost",
"token": "xqastwxo5zghlgjcapmq5tirae",
"desc": "Z/ww2XroGoIG5hrgiWsU1P8YHrv4SxiYHHoojzt9tdc=",
"timestamp": 1412806373651
}
]
}
I'm trying to deserialize it to an Object with ObjectMapper
. Essentially as you can see, it is a List of games.
I have classes like these:
@JsonRootName(value="games")
public class GameJson{
private List<Game> games;
// getters and setters
}
the Game class is here:
public class Game{
private String id;
private String from;
private String to;
private String token;
private String desc;
private Instant timestamp;
// getters and setters
}
In my code, the ObjectMapper
is doing this:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
GameJson json = mapper.readValue(
new FileInputStream(gamesFile), GameJson.class);
Then I get this error:
Can not deserialize instance of com.games.collection.GameJson
out of START_ARRAY
token
I am trying different ways to do this, but coming out with no luck. Can someone please help?
Thanks!
Get rid of
@JsonRootName(value="games")
That annotation identifies the annotated type as the target for the JSON object mapped to a JSON key named "games"
. In your case, that is a JSON array. An array cannot be deserialized into your GameJson
class.
As you stated in the comments, you also need to remove the configuration that enables @JsonRootName
.
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments