【问题标题】:karate fetch response of json using variable空手道使用变量获取json的响应
【发布时间】:2019-08-24 19:05:26
【问题描述】:

在我的响应 json 中,有一个日期字段,每天都会更改。

这是我的 json:

{
    "data": {
        "0000164": {
            "2019-04-03": {
                "VALUE": 26
            }
        }
    },
    "status": 200
}

我使用命令从 json 获取数据:$.data.0000164.2019-04-03.OAK

但我想概括一下情况。我将日期保存在变量中

例子:

* def date = '2019-04-03'
* def hotel = '0000164'

想要通过将酒店和日期字段替换为这些值来获取响应

$.data.hotel.date.OAK

【问题讨论】:

    标签: karate


    【解决方案1】:

    你需要使用jsonPathhttps://stackoverflow.com/a/50855425/10791639

    * def date = '2019-04-03'
    * def hotel = '0000164'
    * def answer = karate.jsonPath(response, "$.data." + hotel + "." + date + ".OAK")
    

    【讨论】:

    • 简单的 JS 也可以工作 * def answer = response.data[hotel][date].OAK
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多