我试图从下面的json响应中捕获ghi[0]
元素,即x
,并用小黄瓜/黄瓜语言将其分配给我的BDD中的一个变量,但它抱怨无法读取属性。
这就是我的捕获方式:
* def xyz = response.results.payload.abc.def.ghi
响应
{
"results": {
"payload": {
"abc": [
{
"def": [
{
"ghi": "x",
},
{
"ghi": "y",
},
{
"ghi": "y",
}
]
}
]
}
}
}
这就是它所抱怨的:
features.blah: [1.1:50] blah.feature:30 - evaluation (js) failed: response.results.payload.abc.def.ghi, javax.script.ScriptException: TypeError: Cannot read property "ghi" from undefined in <eval> at line number 1
这是因为您的访问错误。以下操作有效:
* def xyz = response.results.payload.abc[0].def[0].ghi
* match xyz == 'x'
也就是说,如果你懒得遍历深度嵌套的数据,你可以这样做:
* def xyz = get[0] $..ghi
* match xyz == 'x'
请阅读文档,这将为您节省时间:(https://github.com/karatelabs/karate#get