1 回答
TA贡献1813条经验 获得超2个赞
鉴于您的示例,您可以使用TypeReference并将您的文件描述为Map<String, Map<String, List<BLA>>>
private static final String yamlString =
"data_lists:\n" +
" list1: \n" +
" - AA: true\n" +
" BB: true\n" +
" CC: \"value\"\n" +
" - AA: false\n" +
" BB: true\n" +
" CC: \"value2\"";
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Map<String, Map<String, List<BLA>>> fileMap = mapper.readValue(
yamlString,
new TypeReference<Map<String, Map<String, List<BLA>>>>(){});
Map<String, List<BLA>> dataLists = fileMap.get("data_lists");
List<BLA> blas = dataLists.get("list1");
System.out.println(blas);
}
class BLA {
@JsonProperty("AA")
private boolean aa;
@JsonProperty("BB")
private boolean bb;
@JsonProperty("CC")
private String cc;
@Override
public String toString() {
return aa + "|" + bb + "|" + cc;
}
// Getters/Setters
}
这输出
[true|true|value, false|true|value2]
如果您有这样的列表:
data_lists:
list1:
- AA: true
BB: true
CC: "value"
- AA: false
BB: true
CC: "value2"
list2:
- AA: true
BB: true
CC: "value3"
- AA: false
BB: true
CC: "value4"
您可以将"data_lists"值作为集合获取
Map<String, List<BLA>> dataLists = fileMap.get("data_lists");
Collection<List<BLA>> blas = dataLists.values();
System.out.println(blas);
输出:
[[true|true|value, false|true|value2], [true|true|value3, false|true|value4]]
添加回答
举报