我正在尝试在 Groovy 中使用 Snakeyaml 解析 YAML 文件。目标是创建一个我最终可以搜索的地图。
我正在使用的文件结构如下:
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/*
问题是:当我创建地图并返回值时,该maintainers
值与我希望的格式不同。此外,似乎没有任何字符串值被转换为字符串。输出是:
[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/*']]
我在重新格式化这张地图时一定错过了一些愚蠢的事情。我欢迎所有建议!
第 1 步:将文件加载为地图
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']