首页 文章

如何在空手道中使用eval?

提问于
浏览
1

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

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

1 回答

  • 0

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

相关问题