Karate 空手道-获取一个值,该值以char开头,从json响应的最后一个出现
我试图获取此json中存在的值'F20210518060000'Karate 空手道-获取一个值,该值以char开头,从json响应的最后一个出现,karate,Karate,我试图获取此json中存在的值'F20210518060000' [{ "name": "F20210518000000", "timestamp": "2021-05-18T00:00:00Z", "forecast_from": "2021-05-18T00:00:00Z", "forecast_to": "20
[{
"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”开头的最后一项
到目前为止我的代码:
尝试了不同的方法,但仍然没有得到想要的响应
输出:
有关于如何进一步的提示吗?给你:
* 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,请阅读以下内容:我只是好奇,如果我想要得到第二个姓氏,当我有100+个这样的值时,应该使用什么样的正确方法?@Dhamo easy,使用
karate.filter()
就可以得到长度。如果不清楚,一定要问另一个问题
* def temp = null;
* def fun = function(x){ if (x.name.startsWith('F')) karate.set('temp', x.name) }
* karate.forEach(response, fun)
* match temp == 'F20210518060000'