从groovy中的变量读取YAML组件

kzmpq1sx  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(153)

我有一个代码如下

import groovy.yaml.YamlSlurper
def configYaml = '''\
---
application: "Sample App"
users:
- name: "mrhaki"
  likes:
  - Groovy
  - Clojure
  - Java
- name: "Hubert"
  likes:
  - Apples
  - Bananas
connections:
- "WS1"
- "WS2"
'''
// Parse the YAML.
def config = new YamlSlurper().parseText(configYaml)
def data1 = "users"
def date2 = "name"

println config.users.likes // printing correct 
println config.$data1.$data2 // getting error

我需要打印www.example.com中的元素users.name,并且需要从变量形式打印它。例如“println config.$data1.$data2”

mwecs4sa

mwecs4sa1#

你可以做一些像下面。

def config = new YamlSlurper().parseText(configYaml)
def data1 = "users"
def data2 = "name"

println config.users.likes
println config."$data1"."$data2"

相关问题