你如何在Java中实际解析YAML中的值?

tri*_*nth 6 java parsing yaml

我有一个YAML文件,我正在使用它作为一种配置文件.它是这样的,

tests:
    - category: some_category
      test:
          - name: hello
            key1: value1
            key2: value2

          - name: hithere
            key1: value1
            key2: value2
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情:

for all tests as test:
  print test.name + test.key1
Run Code Online (Sandbox Code Playgroud)

我看过JYaml和SnakeYaml但他们似乎只是从YAML映射到Java对象.YAML不适合这个吗?有没有一种简单的方法来实现这一目标?我需要的是迭代并获取每个的值,类似于DOM遍历.

tri*_*nth 1

好吧,我想通了。我正在使用 YamlBeans,它为此提供了方法: https: //github.com/EsotericSoftware/yamlbeans

  • SnakeYAML 可以用 2 行来解析它: Yaml yaml = new Yaml(); yaml.load(文档); YamlBeams 中的哪些方法在 SnakeYAML 中不可用? (4认同)