如下所示如何遍历响应

时间:2019-06-11 06:16:49

标签: karate

{
    "createDate": "2019-05-15 10:07:44",
    "mak": "pokijoklm",
    "optStatus": "yujuim",
    "partnerAccount": {
        "operatorName": "frftcrtii",
        "partnerCustomerId": "XXXXX",
        "type": "partnerAccount"
    },
    "transactionId": "hjknhijn1",
    "type": "nihnui",
    "updateDate": "2019-05-15 11:20:59"
}

对于数组,我们可以尝试作为response.partneraccount [*]。但我的回应是{}

1 个答案:

答案 0 :(得分:1)

仅当存在数组时才使用[]。只需仔细观察您的JSON结构,您将得到它。尝试将以下内容粘贴到新的Scenario中,然后查看其工作原理:

* def response =
"""
{
    "createDate": "2019-05-15 10:07:44",
    "mak": "pokijoklm",
    "optStatus": "yujuim",
    "partnerAccount": {
        "operatorName": "frftcrtii",
        "partnerCustomerId": "XXXXX",
        "type": "partnerAccount"
    },
    "transactionId": "hjknhijn1",
    "type": "nihnui",
    "updateDate": "2019-05-15 11:20:59"
}
"""
* def partnerAccount = response.partnerAccount
* match partnerAccount == { operatorName: 'frftcrtii', partnerCustomerId: 'XXXXX',  type: 'partnerAccount' }
* def custId = response.partnerAccount.partnerCustomerId
* match custId == 'XXXXX'