我有一个JsonBuilder,我遇到了一些麻烦。我希望输出看起来如下:
"unitTests": {
"testType": "TestNG",
"totalTests": 20,
"failedTests": 2,
"skippedTests": 0,
"failedTestList": [
{
"class": "SomeTestClass"
"method": "someTestMethod"
},
{
"class": "AnotherTestClass"
"method": "anotherTestMethod"
}
]
}
相反,我所看到的是:
"unitTests": {
"testType": "TestNG",
"totalTests": 20,
"failedTests": 2,
"skippedTests": 0,
"failedTestList": [
[
{
"class": "SomeTestClass"
}
],
[
{
"method": "someTestMethod"
}
],
[
{
"class": "AnotherTestClass"
}
],
[
{
"method": "anotherTestMethod"
}
]
]
}
生成JSON文档的代码如下:
def json = new JsonBuilder()
def root = json {
time { $date timestamp }
data {
unitTests {
testType unitType
totalTests totalUnitTests
failedTests failedUnitTests
skippedTests skippedUnitTests
failedTestList(failedUnitTestClass.collect {[class: it]}, failedUnitTestMethod.collect {[method: it]})
}
}
}
答案 0 :(得分:1)
需要同时迭代这两个列表。尝试:
[failedUnitTestClass, failedUnitTestMethod].transpose().collect { [class:it[0], method:it[1]] }
完整示例:
import groovy.json.*
def json = new JsonBuilder()
def failedUnitTestClass = ['cls1', 'cls2', ]
def failedUnitTestMethod = ['m1', 'm2', ]
json.unitTests {
failedTestList([failedUnitTestClass, failedUnitTestMethod].transpose().collect {[class:it[0], method:it[1]]})
}
println JsonOutput.prettyPrint(json.toString())