我有一个YAML文件,如下所示:
---
name:
storage:
documentfiles:
username: rafa
password: hello
我正在尝试获取最后两个username
和password
值。我目前的代码如下。我正在使用Map来存储YAML值,但由于当map.get()
任何超过name
的内容时有多个子项,因此它会给我一个空值。如果我map.get(name)
我得到{storage={documentfiles={username=rafa, password=hello}}}
有谁知道我如何才能正确获取用户名和密码?
public Map grabYaml(){
Yaml reader = new Yaml();
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(yamlFileName);
Map map = (Map) reader.load(inputStream);
return map;
}
答案 0 :(得分:0)
像这样的东西
public class Test {
public Map grabYaml() throws IOException {
Yaml reader = new Yaml();
InputStream inputStream = new FileInputStream(new File(yamlFileName));
Map map = (Map) reader.load(inputStream);
return map;
}
public static void main(String[] args) throws IOException {
Map storage = (Map) new Test().grabYaml().get("name");
Map documentfiles = (Map)storage.get("storage");
Map userData = (Map) documentfiles.get("documentfiles");
System.out.println(userData.get("username"));
System.out.println(userData.get("password"));
}
}