空手道 - 从 json 响应中获取以字符开头并从最后出现的值
Karate - get a value that starts with a char and appears from last from json response
我正在尝试获取此 json
中存在的值 'F20210518060000'
[{
"name": "F20210518000000",
"timestamp": "2021-05-18T00:00:00Z",
"forecast_from": "2021-05-18T00:00:00Z",
"forecast_to": "2021-05-27T00:00:00Z"
},
{
"name": "T20210518000000",
"timestamp": "2021-05-18T00:00:00Z",
"forecast_from": "2021-05-18T00:00:00Z",
"forecast_to": "2021-05-18T06:00:00Z"
},
{
"name": "T20210518060000",
"timestamp": "2021-05-18T06:00:00Z",
"forecast_from": "2021-05-18T06:00:00Z",
"forecast_to": "2021-05-18T12:00:00Z"
},
{
"name": "F20210518060000",
"timestamp": "2021-05-18T06:00:00Z",
"forecast_from": "2021-05-18T06:00:00Z",
"forecast_to": "2021-05-27T06:00:00Z"
},
{
"name": "T20210518120000",
"timestamp": "2021-05-18T12:00:00Z",
"forecast_from": "2021-05-18T12:00:00Z",
"forecast_to": "2021-05-18T18:00:00Z"
},
{
"name": "T20210518180000",
"timestamp": "2021-05-18T18:00:00Z",
"forecast_from": "2021-05-18T18:00:00Z",
"forecast_to": "2021-05-19T00:00:00Z"
}]
条件:
获取 JSON 中以 'F'
开头的最后一项
到目前为止我的代码:
尝试了不同的方式,但仍未得到期望的响应。
输出:
14:10:23.013 [pool-1-thread-1] INFO com.intuit.karate - [print] [
"F20210517180000",
"T20210517180000"]
14:10:23.018 [pool-1-thread-1] INFO com.intuit.karate - [print] 2021-05-18T12:00:00Z
关于如何更进一步的任何提示?
给你:
* def temp = null;
* def fun = function(x){ if (x.name.startsWith('F')) karate.set('temp', x.name) }
* karate.forEach(response, fun)
* match temp == 'F20210518060000'
有时最好避免使用 JsonPath,请阅读:https://github.com/intuit/karate#json-transforms
我正在尝试获取此 json
中存在的值 'F20210518060000'[{
"name": "F20210518000000",
"timestamp": "2021-05-18T00:00:00Z",
"forecast_from": "2021-05-18T00:00:00Z",
"forecast_to": "2021-05-27T00:00:00Z"
},
{
"name": "T20210518000000",
"timestamp": "2021-05-18T00:00:00Z",
"forecast_from": "2021-05-18T00:00:00Z",
"forecast_to": "2021-05-18T06:00:00Z"
},
{
"name": "T20210518060000",
"timestamp": "2021-05-18T06:00:00Z",
"forecast_from": "2021-05-18T06:00:00Z",
"forecast_to": "2021-05-18T12:00:00Z"
},
{
"name": "F20210518060000",
"timestamp": "2021-05-18T06:00:00Z",
"forecast_from": "2021-05-18T06:00:00Z",
"forecast_to": "2021-05-27T06:00:00Z"
},
{
"name": "T20210518120000",
"timestamp": "2021-05-18T12:00:00Z",
"forecast_from": "2021-05-18T12:00:00Z",
"forecast_to": "2021-05-18T18:00:00Z"
},
{
"name": "T20210518180000",
"timestamp": "2021-05-18T18:00:00Z",
"forecast_from": "2021-05-18T18:00:00Z",
"forecast_to": "2021-05-19T00:00:00Z"
}]
条件: 获取 JSON 中以 'F'
开头的最后一项到目前为止我的代码: 尝试了不同的方式,但仍未得到期望的响应。
14:10:23.013 [pool-1-thread-1] INFO com.intuit.karate - [print] [ "F20210517180000", "T20210517180000"] 14:10:23.018 [pool-1-thread-1] INFO com.intuit.karate - [print] 2021-05-18T12:00:00Z
关于如何更进一步的任何提示?
给你:
* def temp = null;
* def fun = function(x){ if (x.name.startsWith('F')) karate.set('temp', x.name) }
* karate.forEach(response, fun)
* match temp == 'F20210518060000'
有时最好避免使用 JsonPath,请阅读:https://github.com/intuit/karate#json-transforms