gpt4 book ai didi

java - 使用 Jackson 解析 Json 文件时无法反序列化 parseJason 实例

转载 作者:行者123 更新时间:2023-11-30 07:05:23 25 4
gpt4 key购买 nike

我有一个 Json 文件:

[
{
"name":"Move",
"$$hashKey":"object:79",
"time":11.32818,
"endTime":18.615535
},
{
"name":"First Red Flash",
"$$hashKey":"object:77",
"time":15.749153
},
{
"name":"Pills",
"subEventTypes":[
"pull down bottle",
"unscrew lid",
"dump pills out",
"screw lid on",
"put bottle away"
],
"$$hashKey":"object:82",
"time":25.130175,
"subEventSplits":[
26.092057,
27.425881,
31.841594,
34.268093
],
"endTime":36.234827
}


]

我尝试使用 Jackson.json 解析此 Json 文件。我编写了以下代码:

public class Holder{
public Holder(){};
//getter and setters

String name;
List<String> subEventTypes = new ArrayList<>();
Double time;
String $$hashKey;
Double endTime;
List<Double> subEventSplits = new ArrayList<>();

}
class MapperClass{
List<Holder> list = new ArrayList<>();
}

public static void main(String[] args) throws JsonProcessingException, IOException
{
ObjectMapper mapper = new ObjectMapper();
List<Holder> list = mapper.readValue(new File("data.json"), mapper.getTypeFactory().constructCollectionType(
List.class, Holder.class));

}

当我运行该程序时,它显示此错误:“

No suitable constructor found for type [simple type, class parseJason$Holder]: can not instantiate from JSON object (need to add/enable type information?)

”。我的代码有什么问题吗?或者我必须使用另一种方法来解析我的 Json 文件。

最佳答案

尝试

list = mapper.readValue(
jsonString,
objectMapper.getTypeFactory().constructCollectionType(
List.class, Holder.class));

关于java - 使用 Jackson 解析 Json 文件时无法反序列化 parseJason 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40231297/

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