简体   繁体   中英

Rest-assured looking up object from Unnamed Root

I am relatively new to Rest-assured and Gpath. From the JSON below, I just want to get a map with the keys / values for ' RideA '

[{
"state": "open",    
"name": "RideA",
"imageCount": 2
}, 
{
"state": "open",
"name": "RideB",    
"imageCount": 1    
},
{
"state": "open",    
"name": "RideC",    
"imageCount": 2
}]

so far I have tried:

final List<Map<String, ?>> object = from(json).get("it.findAll { it.name == 'RideA' }");

but this returns java.lang.IllegalArgumentException: Cannot get property 'name' on null object , so I think there is something wrong with my syntax

The code below works, but I have to look up the elements with the list by their ordinal number (in this case '0'), when I want to look them up by the 'name' field

final List<Map<String,?>> object = from(json).get("");
assertThat(object.get(0).get("name"), IsEqual.<Object>equalTo("RideA"));

any help is much appreciated!

I finally managed to figure this out, thanks to help from Johan Haleby.

firstly there was a slight error in my first statement, I should have just been looking for a Map, and not a List of Maps.

final Map<String, ?> object = from(json).get("it.findAll { it.name == 'RideA' }");

'findAll' returns a list, so that would not be correct in this case.

The correct statement in this case would be:

final Map<String, ?> object = from(json).get("find { it.name == 'RideA'}");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM