gpt4 book ai didi

java - YAML 值未正确返回

转载 作者:太空宇宙 更新时间:2023-11-04 13:45:27 24 4
gpt4 key购买 nike

我有一个如下所示的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com