如何在空手道中使用 eval?

How to use eval in karate?

我已经阅读了空手道文档并尝试了以下代码:

 * eval if (nearby.content[?(@.title == 'Nearby Malls & Restaurants')] == '#notnull') karate.call(* def nearByMallsRestraurants = get nearby.content[?(@.title == 'Nearby Malls & Restaurants')].items[?(@.name)])

但是如果附近的地点和地标数组包含附近的购物中心,它的抛出 error.I 正在尝试提取所有附近的购物中心和餐馆的名称 & Restaurant.Also 如果可能的话,你能告诉我如何使用 collection.sort 对附近的购物中心名称进行排序。

我的 json 看起来像:

"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
              }
            ]
          }
        ]
      }

请注意 - JsonPath 不是 JS,您在这里混用了东西。我的建议是不要将事情过于复杂化并将其分成多个步骤。这应该可以回答您的所有问题,包括排序:

* 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

输出:

[
  "Eggzotic",
  "Forum Mall, Koramangala",
  "Kerala Pavilion Restaurant",
  "New Shanthi Nagar",
  "Venus Biryani"
]