Groovy: 为什么节点返回 null
Groovy: Why the node is returning null
我想将我的 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 : [空, 空, 空]
代替 "create an array, call addAll in a loop" 模式,试试这个:
def array = Screen.collectMany { s ->
[s.Rate,s.Primary,s.Pre]
}
(当然,一旦您从 JSON 键中删除了空格)
我想将我的 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 : [空, 空, 空]
代替 "create an array, call addAll in a loop" 模式,试试这个:
def array = Screen.collectMany { s ->
[s.Rate,s.Primary,s.Pre]
}
(当然,一旦您从 JSON 键中删除了空格)