【发布时间】:2014-10-10 15:14:16
【问题描述】:
我有一个这样的 json:
{
"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
}
]
}
我正在尝试将其反序列化为带有ObjectMapper 的对象。正如您所看到的,它本质上是一个游戏列表。
我有这样的课程:
@JsonRootName(value="games")
public class GameJson{
private List<Game> games;
// getters and setters
}
游戏类在这里:
public class Game{
private String id;
private String from;
private String to;
private String token;
private String desc;
private Instant timestamp;
// getters and setters
}
在我的代码中,ObjectMapper 正在这样做:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
GameJson json = mapper.readValue(
new FileInputStream(gamesFile), GameJson.class);
然后我得到这个错误:
无法从 START_ARRAY 令牌中反序列化 com.games.collection.GameJson 的实例
我正在尝试不同的方法来做到这一点,但没有运气。有人可以帮忙吗?
谢谢!
【问题讨论】:
标签: java json jackson deserialization