当这是我的.yml文件时:
test1: "string1"
test2:
test3: "string2"如何获得test3的值
Map<String, Object> yamlFile = new Yaml().load(YamlFileInputStream);
yamlFile.get("test1"); // output: string1
yamlFile.get("test2"); // output: {test3=string2}
yamlFile.get("test2.test3"); // output: key not found发布于 2020-02-09 16:20:33
YAML没有“堆叠键”。它有嵌套映射。点.不是一个特殊的字符,可以在键中正常出现,因此您不能使用它来查询嵌套映射中的值。
您已经展示了如何访问包含test3键的映射,只需查询其中的值:
((Map<String, Object)yamlFile.get("test2")).get("test3");但是,将YAML文件的结构定义为类要简单得多:
class YamlFile {
static class Inner {
public String test3;
}
public String test1;
public Inner test2;
}然后您可以像这样加载它:
YamlFile file = new Yaml(new Constructor(YamlFile.class)).loadAs(
input, YamlFile.class);
file.test2.test3; // this is your stringhttps://stackoverflow.com/questions/60135911
复制相似问题