收到错误groovy.lang.MissingPropertyException:即使存在值,也没有这样的属性

时间:2016-08-22 13:15:26

标签: json groovy soapui

值存在于响应中,可以通过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

1 个答案:

答案 0 :(得分:2)

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

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