Automated tests 无法从JSON元素读取属性

Automated tests 无法从JSON元素读取属性,automated-tests,karate,Automated Tests,Karate,我试图从有效负载中的属性中获取值,但是空手道抛出了一个错误,或者根本没有获取值 我已经创建了一个简化版本的代码,使其更容易理解 * def lists = [{@id: 1, type: 'video'}, {@id: 2, type: 'image'}] * def ser = {@id: 2, type: '#string'} * def foundAt = [] * def fun = function(x, i){ if (karate.match(x, ser).pass) foun

我试图从有效负载中的属性中获取值,但是空手道抛出了一个错误,或者根本没有获取值

我已经创建了一个简化版本的代码,使其更容易理解

* def lists = [{@id: 1, type: 'video'}, {@id: 2, type: 'image'}]
* def ser = {@id: 2, type: '#string'}

* def foundAt = []
* def fun = function(x, i){ if (karate.match(x, ser).pass) foundAt.add(i) }
* eval karate.forEach(lists, fun)
* def storeId = lists[foundAt[0]].@id
* def storeType = lists[foundAt[0]].type
* print storeId
* print storeType
print storeType将按预期打印值,但print storeId将打印以下错误消息:

javascript evaluation failed: lists[foundAt[0]].@id, <eval>:1:18 Expected ident but found error
lists[foundAt[0]].@id
                  ^ in <eval> at line number 1 at column nu*mber 18
javascript评估失败:列出[foundAt[0].@id,:1:18预期的标识,但发现错误
列出[foundAt[0].@id
^在第18列nu*mber的第1行中

我希望打印值“2”,但显然我做错了什么?

一个小改动就可以了,因为
@
对于JSON键名来说是一个“坏”字符:

* def storeId = lists[foundAt[0]]['@id']
这里还有一个简化代码的建议:

* def fun = function(x, i){ return karate.match(x, ser).pass }
* def found = karate.filter(lists, fun)
* def storeId = found[0]['@id']
* def storeType = found[0].type
* print storeId
* print storeType