Groovy:为什么节点返回null

时间:2016-08-22 09:39:58

标签: json groovy soapui

我想将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]

1 个答案:

答案 0 :(得分:3)

而不是"创建一个数组,在循环" 模式中调用addAll,试试这个:

def array = Screen.collectMany { s ->
    [s.Rate,s.Primary,s.Pre]
}

(当然,一旦你从JSON密钥中删除了空格)

相关问题