我有一个如下所示的 YAML 文件:
---
name:
storage:
documentfiles:
username: rafa
password: hello
我正在尝试获取最后两个用户名
和密码
值。我当前的代码如下。我使用 Map 来存储 YAML 值,但由于当我 map.get()
任何过去 name
的内容时,有多个子项,因此它给了我一个 null 值。如果我这样做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;
}
最佳答案
类似这样的事情
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"));
}
}
关于java - YAML 值未正确返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30896480/