gpt4 book ai didi

java - ObjectMapper 无法反序列化 - 无法反序列化 START_ARRAY token 之外的 .... 实例

转载 作者:行者123 更新时间:2023-12-02 05:23:30 35 4
gpt4 key购买 nike

我有一个像这样的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将其反序列化为对象。本质上如您所见,它是一个游戏列表。

我有这样的类(class):

@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 token 中反序列化 com.games.collection.GameJson 实例

我正在尝试不同的方法来做到这一点,但没有成功。有人可以帮忙吗?

谢谢!

最佳答案

摆脱

@JsonRootName(value="games")

该注释将注释类型标识为映射到名为 “games” 的 JSON 键的 JSON 对象的目标。在你的例子中,这是一个 JSON 数组。数组无法反序列化到您的 GameJson 类中。

As you stated in the comments ,您还需要删除启用 @JsonRootName 的配置。

mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

关于java - ObjectMapper 无法反序列化 - 无法反序列化 START_ARRAY token 之外的 .... 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26302613/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com