我试图在Groovy中使用snakeyaml解析一个YAML文件。我们的目标是创建一个Map,我最终可以搜索。
我使用的文件结构如下:
maintainers:
- component: PowerPuff Girls
pg: Utonium Labs
people:
- Buttercup
- Blossom
- Bubbles
files:
- sugar/*
- spice/*
- things/*nice*
- chemicalx/*
- component: Gangreen Gang
pg: Villians
people:
- Ace
- Snake
- Big Billy
files:
- ace/*
- snake/*
问题是:当我创建一个map并返回值时,maintainers
的值与我所希望的格式不符。此外,似乎没有一个String值被转换为String。输出为:
[maintainers:[[component:PowerPuff Girls, pg:Utonium Labs, people:[Buttercup, Blossom, Bubbles], files:[sugar/*, spice/*, things/*nice*, chemicalx/*]], [component:Gangreen Gang, pg:Villians, people:[Ace, Snake, Big Billy], files:[ace/*, snake/*]]]]
在一个完美的世界里,我会:
[component: 'PowerPuff Girls', pg: 'Utonium Labs', people: ['Buttercup', 'Blossom', 'Bubbles'], files: ['sugar/*','spice/*','things/*nice*', 'chemicalx']],
[component: 'Gangreen Gang', pg: 'Villians', people: ['Ace', 'Snake', 'Big Billy'], files: ['ace/*','snake/*']]
我一定是错过了一些愚蠢的重新格式化这张Map。欢迎所有建议!
步骤1:将文件加载为Map
Yaml yaml = new Yaml()
Map yamlMap = yaml.load(yamlFile)
步骤2:返回map以观察格式return yamlMap
结果:
[maintainers:[[component:PowerPuff Girls, pg:Utonium Labs, people:[Buttercup, Blossom, Bubbles], files:[sugar/*, spice/*, things/*nice*, chemicalx/*]], [component:Gangreen Gang, pg:Villians, people:[Ace, Snake, Big Billy], files:[ace/*, snake/*]]]]
我试图分别定义各个键和值并重新构造它们,但它并没有像预期的那样提取分组。例如:def myVariable = yamlMap['maintainers']['component']
1条答案
按热度按时间bkhjykvo1#
最接近
ideal world
的可能是json。如果要搜索
component
匹配某个值项目:列表/集合上的更多groovy方法:
https://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/List.html