【问题标题】:How to use eval in karate?如何在空手道中使用 eval?
【发布时间】:2018-12-04 20:18:14
【问题描述】:

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

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

但是它的抛出错误。如果附近的地方和地标数组包含附近的购物中心和餐厅,我正在尝试提取所有附近的购物中心和餐厅的名称。如果可能的话,请告诉我如何使用 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
              }
            ]
          }
        ]
      }

【问题讨论】:

    标签: karate


    【解决方案1】:

    请注意 - 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"
    ]
    

    【讨论】:

    • 谢谢@Peter。但我试图提取所有Nearby Malls & Restaurants 名称,前提是Nearby Places and Landmarks 包含Nearby Malls & Restaurants json,因为Nearby Malls & Restaurants 不是强制性的。
    • @GauravKinra 这绝对没问题,你试过了吗?添加这一行,看看如果你没有商场会发生什么:* eval if (names.length == 0) karate.log('no malls found')
    • 嗨彼得,现在只需要一个与此相关的小帮助我有一个返回数据price": { "pretax_price": 7002.84, "rack_rate": 8140.82, "wallet_deductable_amount": 0, "voucher_amount": 0, "total_discount_percent": 92.75, "net_payable_amount": 4131.68, }的api。现在我想做的是* def pretax =price.pretax_price只有在@987654330时才应该被调用@ 其中valueFromDB 来自数据库。
    • @GauravKinra 请在新问题中执行此操作。
    猜你喜欢
    • 1970-01-01
    • 2022-01-17
    • 2018-12-14
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    相关资源
    最近更新 更多