问题
I already gone through the karate documentation and tried below code:
* eval if (nearby.content[?(@.title == 'Nearby Malls & Restaurants')] == '#notnull') karate.call(* def nearByMallsRestraurants = get nearby.content[?(@.title == 'Nearby Malls & Restaurants')].items[?(@.name)])
But its throwing error.I am trying to extract all Nearby Malls & Restaurants names if Nearby Places and Landmarks Array contains Nearby Malls & Restaurant.Also if possible can you please tell me how to use collection.sort
to get sorted Nearby Malls name.
My json looks like :
"nearby": {
"title": "Nearby Places and Landmarks",
"content": [
{
"title": "Nearby Malls & Restaurants",
"items": [
{
"name": "Forum Mall, Koramangala",
"distance": 1.8
},
{
"name": "Eggzotic",
"distance": 2.4
},
{
"name": "Kerala Pavilion Restaurant",
"distance": 2.4
},
{
"name": "New Shanthi Nagar",
"distance": 2.5
},
{
"name": "Venus Biryani",
"distance": 2.8
}
]
},
{
"title": "Closest Airport, Railway Station & Bus Stand",
"items": [
{
"name": "Madiwala Ayyappa Temple Bus Stop",
"distance": 2.1
},
{
"name": "Kalasipalyam Bus Stand",
"distance": 5.7
},
{
"name": "Bangalore Cantonment Railway Station",
"distance": 6.5
},
{
"name": "Kempegowda/ Majestic bus station",
"distance": 7
},
{
"name": "KSR Bengaluru City Railway Station",
"distance": 7.5
},
{
"name": "KR Puram Railway Station",
"distance": 8.5
},
{
"name": "KSRTC Mysore Road Satellite Bus Stop",
"distance": 9
}
]
}
]
}
回答1:
Please note - JsonPath is not JS and you are mixing things here. My advice is don't over-complicate things and split it into multiple steps. This should answer all your questions, including a sort:
* def malls = $nearby.content[?(@.title=='Nearby Malls & Restaurants')]
* def names = $malls..name
* def Collections = Java.type('java.util.Collections')
* eval Collections.sort(names)
* print names
output:
[
"Eggzotic",
"Forum Mall, Koramangala",
"Kerala Pavilion Restaurant",
"New Shanthi Nagar",
"Venus Biryani"
]
来源:https://stackoverflow.com/questions/51037311/how-to-use-eval-in-karate