2016-08-22 1 views
0

Werte sind als Antwort vorhanden, können sie über log.info ausgeben, geben mir aber einen Fehler, wenn ich sie im Array hinzufüge, hier ist mein grooviges Skript,Fehler bekommen groovy.lang.MissingPropertyException: Keine solche Eigenschaft, auch wenn Werte vorhanden sind

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 

und die Fehlermeldung,

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

Antwort

2

Die collectMany iteriert über Schlüssel/Wert-Paaren. Betrachten Sie Folgendes (so weit ich das Ziel verstehe):

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 
+0

danke @Michael Ostern – Gkm

Verwandte Themen