Arrays Groovy ConfigSlurper配置阵列

Arrays Groovy ConfigSlurper配置阵列,arrays,groovy,config,Arrays,Groovy,Config,我正在尝试创建一个如下所示的配置: nods = [ nod { test = 1 }, nod { test = 2 } ] config {sha=[{from = 123;to = 234},{from = 234;to = 567}]} 然后使用configSlurper读取,但读取后“节点”对象显示为空 这是我的密码: final ConfigObject data = new ConfigSlurper().pars

我正在尝试创建一个如下所示的配置:

nods = [
    nod {
        test = 1
    },
    nod {
        test = 2
    }
]
config {sha=[{from = 123;to = 234},{from = 234;to = 567}]}
然后使用configSlurper读取,但读取后“节点”对象显示为空

这是我的密码:

final ConfigObject data = new ConfigSlurper().parse(new File("config.dat").toURI().toURL())
println  data.nods
以及输出:

[null, null]
我做错了什么


谢谢

我想我是这样解决的:

config {
   nods = [
      ['name':'nod1', 'test':true],
      ['name':'nod2', 'test':flase]
   ]
}
然后像这样使用它:

config = new ConfigSlurper().parse(new File("config.groovy").text)
for( i in 0..config.config.nods.size()-1)
    println config.config.nods[i].test

希望这对其他人有帮助

在进行此类操作时,使用ConfigSlurper时必须小心。
例如,您的解决方案将实际产生以下输出:

true
[:]
如果仔细观察,您会注意到第二个数组值flase上有一个打字错误,而不是false

以下是:

def configObj = new ConfigSlurper().parse("config { nods=[[test:true],[test:false]] }")
configObj.config.nods.each { println it.test }
应产生正确的结果:

true
false

我尝试使用ConfigSlurper进行如下分析:

nods = [
    nod {
        test = 1
    },
    nod {
        test = 2
    }
]
config {sha=[{from = 123;to = 234},{from = 234;to = 567}]}
数组“sha”与预期相差甚远。 为了将“sha”作为ConfigObjects数组,我使用了一个助手:

class ClosureScript extends Script {
    Closure closure

    def run() {
        closure.resolveStrategy = Closure.DELEGATE_FIRST
        closure.delegate = this
        closure.call()
    }
}
def item(closure) {
    def eng = new ConfigSlurper()
    def script = new ClosureScript(closure: closure)
    eng.parse(script)
}
这样我就得到了一个ConfigObject数组:

void testSha() {
    def config = {sha=[item {from = 123;to = 234}, item {from = 234;to = 567}]}
    def xx = item(config)
    assertEquals(123, xx.sha[0].from)
}