如何使用 snakeYaml 库访问 YAML 文件中的内部(嵌套)键值

How to Access the inner(nested) key-value in an YAML file using snakeYaml Library

我正在尝试使用 java 中的 snakeYaml 库读取 config.yaml 文件。

我能够在我的配置文件中获取模块名称(即 [{ABC=true}, {PQR=false}])。 有没有一种方法可以使用代码直接读取 ABC 的值(即 true)。

我曾尝试在线搜索,但它们并不是我要找的。 我在下面提到的几个链接:

Load .yml file into hashmaps using snakeyaml (import junit library)

https://www.java-success.com/yaml-java-using-snakeyaml-library-tutorial/

config.yaml数据:

Browser: FIREFOX
Module Name:
- ABC: Yes
- PQR: No

下面是我使用的代码

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.util.Map;
import org.yaml.snakeyaml.Yaml;

public class YAMLDemo {

    public static void main(String[] args) throws FileNotFoundException {

        Yaml yaml = new Yaml();
        Reader yamlFile = new FileReader("./config.yaml");

        Map<String , Object> yamlMaps = yaml.load(yamlFile);

        System.out.println(yamlMaps.get("Browser"));
        System.out.println(yamlMaps.get("Module Name"));
    }
}

控制台输出:

FIREFOX
[{ABC=true}, {PQR=false}]

非常感谢您的帮助。提前致谢。

如果您使用调试器单步执行代码,您会看到 module_name 被反序列化为 ArrayList<LinkedHashMap<String, Object>>

您只需将其转换为正确的类型:

public static void main(String[] args) throws FileNotFoundException {
    Yaml yaml = new Yaml();
    Reader yamlFile = new FileReader("./config.yaml");

    Map<String , Object> yamlMaps = (Map<String, Object>) yaml.load(yamlFile);

    System.out.println(yamlMaps.get("Browser"));
    final List<Map<String, Object>> module_name = (List<Map<String, Object>>) yamlMaps.get("Module Name");
    System.out.println(module_name);
    System.out.println(module_name.get(0).get("ABC"));
    System.out.println(module_name.get(1).get("PQR"));
}

@Devstr 为您提供了关于如何弄清楚数据结构的很好的描述。 这里有一个示例,说明如何将值读入属性对象:

final Properties modules = new Properties();

final List<Map<String, Object>> values = (List<Map<String, Object>>) yamlMaps.get("Module Name");

values.stream().filter(Objects::nonNull)
               .flatMap(map -> map.entrySet().stream())
               .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
               .forEach((key, value) -> {
                   modules.put(key, value);
               });

System.out.println(modules.get("ABC"));
System.out.println(modules.get("PQR"));
public class YamlParser {

    public YamlParser() {
    }

    public void parse(Map<String, Object> item, String parentKey) {
    for (Entry entry : item.entrySet()) {
        if (entry.getValue() != null && entry.getValue() instanceof Map) {
        parse((Map<String, Object>) entry.getValue(),
                (parentKey == null ? "" : parentKey + ".") + entry.getKey().toString());
        } else {
        System.out.println("map.put(\"" + (parentKey == null ? "" : parentKey + ".") + entry.getKey() + "\",\""
                + entry.getValue() + "\");");
        }
    }
    }

    public static void main(String[] args) {
    try {
        Yaml yaml = new Yaml();
        InputStream inputStream = new FileInputStream(
                "path-to-application.yml");
        Map<String, Object> obj = yaml.load(inputStream);
        YamlParser parser = new YamlParser();
        parser.parse(obj, null);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    }
}