简体   繁体   English

Groovy YAML解析

[英]Groovy YAML parsing

I am trying to parse some yaml from a file with Groovy here is what I currently have:我正在尝试从带有 Groovy 的文件中解析一些 yaml 这是我目前拥有的:

import groovy.yaml.YamlSlurper
def yamlFile = new File('./test.yaml')
def inputYaml = new YamlSlurper().parse(yamlFile)
inputYaml.each{ println it }

The above prints the yaml file, also below is my yaml file上面打印了 yaml 文件,下面也是我的 yaml 文件

HOST-0:
  tags:
    name: 'HOST0'
    size: 'small'
    region: 'west'
HOST-1:
  tags:
    name: 'HOST0'
    size: 'small'
    region: 'west'

What I'm wonder is if it is possible to create a separate yaml object that would lookup certain values, so lets say I wanted to create a new yaml file that would look like:我想知道是否可以创建一个单独的 yaml object 来查找某些值,所以假设我想创建一个新的 yaml 文件,如下所示:

HOST-0:
  hostname: 'HOST0'

Normally I would be able to do a lookup which would lookup which would look something like HOST-0:tags:host just wondering if it's possible to do this sort of lookup on each yaml object ie HOST-0 and HOST-1 without specifying them or if anyone has any ideas通常我可以做一个查找,它看起来像 HOST-0:tags:host 只是想知道是否可以在每个 yaml object 即 HOST-0 和 HOST-1 上进行这种查找而不指定它们或者如果有人有任何想法

def outYaml = inputYaml.collectEntries{ k,v-> [k, [hostname:v.tags.name] ] }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM