我的 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 文件? 到目前为止,这是我的代码:
File file = new File("data.json");
JsonFactory jsonFactory = new JsonFactory();
JsonParser parser = jsonFactory.createJsonParser(file);
while(parser.nextToken()!=JsonToken.END_OBJECT)
{
//how to read each token
}
最佳答案
Jackson 应该解析您提到的 JSON 文件。创建一个类,如下所示
Class Holder {
String name;
List<String> subEventTypes = new ArrayList<>();
String $$hashKey;
Double time;
List<Double> subEventSplits = new ArrayList<>()
DOuble endTime;
}
Class MapperClass{
List<Holder> list = new ArrayList<>();
}
解析如下图
ObjectMapper mapper = new ObjectMapper();
//JSON from file to Object
MapperClass values = mapper.readValue(new File("c:\\values.json"), MapperClass.class);
引用这个link了解更多详情
关于java - 使用 Jackson 解析具有多个对象的 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40210804/