如何在空手道中使用eval? [英] How to use eval in karate?

查看:70
本文介绍了如何在空手道中使用eval?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)])

但是它抛出错误.我正在尝试提取附近的所有购物中心.如果附近的地标"数组包含附近的购物中心",则为餐厅名称.餐厅.如果可以的话,请告诉我如何使用collection.sort获取附近购物中心的名称.

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.

我的json看起来像:

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

推荐答案

请注意-JsonPath不是JS,您在这里混用.我的建议是不要使事情过于复杂,而是将其分为多个步骤.这应该回答您所有的问题,包括排序:

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

输出:

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

这篇关于如何在空手道中使用eval?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆