【问题标题】:How to Access the inner(nested) key-value in an YAML file using snakeYaml Library如何使用 snakeYaml 库访问 YAML 文件中的内部(嵌套)键值
【发布时间】:2018-07-22 12:44:55
【问题描述】:

我正在尝试使用 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}]

非常感谢任何帮助。提前致谢。

【问题讨论】:

    标签: java yaml snakeyaml


    【解决方案1】:

    如果您使用调试器单步执行代码,您可以看到 module_name 被反序列化为 ArrayList&lt;LinkedHashMap&lt;String, Object&gt;&gt;

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

    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"));
    }
    

    【讨论】:

    • 非常感谢。有效。还学会了在调试控制台中更深入地查看。干杯。
    【解决方案2】:
    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();
        }
        }
    }
    

    【讨论】:

      【解决方案3】:

      @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"));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-10
        • 2014-09-16
        • 2019-12-18
        • 2016-05-15
        • 2014-04-11
        • 1970-01-01
        • 2015-08-31
        相关资源
        最近更新 更多