我有以下JSON响应-
{
"type": "StudentSchema",
"version": 1,
"students": [
{
id: 1,
name: "John",
roll: "1234"
},
{
id: 2,
name: "David",
roll: "4434"
}
]
}
然后,如何在空手道中提取名为John的数组以进行进一步验证?例如,我想说如果name == John然后保存ID
我在下面尝试,但似乎不起作用-
* def userId = get[0] response $[?(@students.name == 'John')].id
* match userId == 2
假设您的JSON是
MyJson = {
"type": "StudentSchema",
"version": 1,
"students": [
{
id: 1,
name: "John",
roll: "1234"
},
{
id: 2,
name: "David",
roll: "4434"
}
]
}
现在,当您想要获取姓名为john的学生的ID时,可以使用JSON路径获取它
* def JSONpath = '$..students[?(@.name=='John')].id'
* def userId = karate.jsonPath(MyJson,JSONpath)
它将为您提供一个满足json路径条件的ID数组,您可以从中进行声明。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句