出现错误 groovy.lang.MissingPropertyException:没有这样的 属性 即使存在值

getting an error groovy.lang.MissingPropertyException: No such property even when values are present

响应中存在值,可以通过 log.info 打印它们,但是在将它们添加到数组中时出现错误,这是我的 groovy 脚本,

import groovy.json.*
def ResponseMessage = ''' {
"Unit": {
    "Profile": 12,
    "Name": "Geeta"
},
"UnitID": 2
} '''
def json = new JsonSlurper().parseText(ResponseMessage)
log.info  json.UnitID
log.info  json.Unit.Profile
log.info  json.Unit.Name

def arrayjson = json.collectMany { s ->
[s.UnitID,s.Unit.Profile,s.Unit.Name]
}

log.info "arrayjson : " + arrayjson 

和错误消息,

groovy.lang.MissingPropertyException: No such property: UnitID for class: java.util.HashMap$Entry Possible solutions: key error at line: 14

collectMany 遍历 key/value 对。考虑以下(据我了解目标):

import groovy.json.*

def ResponseMessage = ''' {
"Unit": {
    "Profile": 12,
    "Name": "Geeta"
},
"UnitID": 2
} '''

def json = new JsonSlurper().parseText(ResponseMessage)
println json.UnitID
println json.Unit.Profile
println json.Unit.Name

// this illustrates how collectMany works, though it does 
// not solve the original goal
json.collectMany { key, val -> 
    println "key: ${key} , val: ${val}"
    [] 
}

def arrayjson = [json.UnitID,json.Unit.Profile,json.Unit.Name]

println "arrayjson : " + arrayjson