我有一个YAML,应该将其解析为JAVA POJO对象,而不是抛出java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to <mytype>
。 YAML如下:
---
-
count: 1
indicies:
- be-log-2018.09.20
searchText: someText
services:
- um
- sa
-
count: 2
searchText: someMoreText
services:
- um2
- sa2
-
count: 2
searchText: someMoreText
我的POJO课是:
public class MyType {
private int count;
private String searchText;
private String[] services;
private String[] indicies;
并使用读取YAML:
ObjectMapper mapper= new ObjectMapper(new YAMLFactory());
mapper.readValue(new File(path), List.class);
理想情况下,如果没有数据,我希望有一个空数组。
答案 0 :(得分:0)
使用此
List<MyType> myTypeList = objectMapper.readValue(new File(path), new TypeReference<List<MyType>>() {});
详细了解TypeReference