我想将json响应值添加到数组中。我的groovy脚本,
import groovy.json.*
def ResponseMessage = '''{
"Unit": {
"Screen": [{
"Profile ": {
"ID ": 12,
"Rate ": 0
},
"Rate ": 600,
"Primary ": 1,
"Audio ": [{
"Id ": 1,
"Name ": null
}],
"Pre ": 5,
"Post ": 1
}]
}
} '''
def json = new JsonSlurper().parseText(ResponseMessage)
def Screen = json.Unit.Screen
log.info Screen
def array= []
Screen.each { s ->
array.addAll(s.Rate,s.Primary,s.Pre)
log.info "array : " + array
}
数组正在返回 INFO:array:[null,null,null]
答案 0 :(得分:3)
而不是"创建一个数组,在循环" 模式中调用addAll,试试这个:
def array = Screen.collectMany { s ->
[s.Rate,s.Primary,s.Pre]
}
(当然,一旦你从JSON密钥中删除了空格)