从空手道中的 Get 方法,我得到了 json 格式的响应。 以下是响应的前几行。 { “状态代码”:200, “isBase64Encoded”:假, “标题”:{ “内容类型”:“应用程序/json” }, "body": "{"result": "{'merchant_id': 609125186933, 'charge_month_duration': ['201801', '201801'],..... }"}" }
从上面我想在空手道打印语句中打印merchant_id或charge_month_duration的值。 我尝试了如下代码
场景:
直到这里我才得到实际结果。 但是当我尝试返回 result 或merchant_id 的值时,它返回 null。
JSON 看起来设计得很糟糕。我假设这就是它应该的样子:
* def response =
"""
{
"statusCode": 200,
"isBase64Encoded": false,
"headers": {
"Content-Type": "application/json"
},
"body": {
"result": "{'merchant_id': 609125186933, 'charge_month_duration': ['201801', '201801'] }"
}
}
"""
然后你可以这样做:
* def result = karate.fromString(response.body.result)
* match result.merchant_id == 609125186933
* match result.charge_month_duration[0] == '201801'