1

我正在使用空手道自动化工具进行服务自动化.. 我正在尝试从我存储在变量中的 json 响应中提取部门 ID...

def departmentId = getstorewalk.departments[*].id

getstorewalk 是我存储 json 响应的变量

以下是json响应

{"walkzz":"001","zz":zz,"ddd":"zz","zz":{"zz":"zz","who":{"zz":"11"," zz":"zz"}},"departments":[{"id":需要提取这个id,"name":"zz","someorder":1,"zzs":[{"zz":zz ,"name":"zz (zz, zz, zz, zz & zz)","someorder":zz,"zz":[{"zz":51,"name":"zz Spread","someorder" :16,"zz":"可用","zz":[{"zz":"1223","zz":"zz 30g","zz":3,"zz":0,"zz": "陈旧","zz":false,"zz":true,"zz":[],"zz":{"zz":2,"zz":"zz","zz":6,"zzName":"核心 zz (zz)","department":56,"zz":"015 zz Spreads","zz":"zz Spread","subzz":100," somezz":"zz & zz","zz":{},"zz_":100},"zz":{"zz":"2017-09-21T11:09:15.524Z","who":{ "zz":"11","zz":"zz"}},"action":{"zz":"Include"},"zz":[{"capturezz":375716,"zz":"差距","qty":15,"zz":"zz","zz":{"zz":"zz","type":"N","name":"zz","sequence":1 },"zz":{"zz":"211","who":{"zz":"11","zz":"zz"}}}]}]}]}]}]}

我在后台使用了以下内容:

  • def getstorewalk = callonce read('classpath:zz/zz/zz.feature')
  • def departmentId = getstorewalk.departments[*].id

下面列出了错误:com.jayway.jsonpath.PathNotFoundException:com.jayway 的 com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:72) 的路径 $['departments'] 中缺少属性。 jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:77) 在 com.jayway.jsonpath.internal.path.RootPathToken.evaluate(RootPathToken.java:62) 在 com.jayway.jsonpath.internal.path.CompiledPath。在 com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:61) 在 com.jayway.jsonpath.JsonPath.read(JsonPath.java:187) 在 com.jayway 评估(CompiledPath.java:53) .jsonpath.internal.JsonContext.read(JsonContext.java:164) 在 com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:151) 在 com.intuit.karate.Script.evalJsonPathOnVarByName(Script.java:339 ) 在 com.intuit.karate.Script.eval(Script.java:262) 在 com.intuit.karate.Script.eval(Script.java:182) 在 com.intuit.karate.Script.assign(Script.java:606) 在 com .intuit.karate.Script.assign(Script.java:537) at com.intuit.karate.StepDefs.def(StepDefs.java:268) at ?.* def departmentId = getstorewalk.departments[*].id(C: /空手道/zz/zz/src/test/java/zz/zz/zz.feature:11)

4

1 回答 1

0

这对我有用,您可以尝试将下面的 3 行粘贴到空手道文件中。顺便说一句,这是一个很好的故障排除技巧,您可以测试这样的代码片段而无需进行 HTTP 调用。

* def getstorewalk = {"walkzz":"001","zz":zz,"ddd":"zz","zz":{"zz":"zz","who":{"zz":"11","zz":"zz"}},"departments":[{"id":need to extract this id,"name":"zz","someorder":1,"zzs":[{"zz":zz,"name":"zz (zz, zz, zz, zz & zz)","someorder":zz,"zz":[{"zz":51,"name":"zz Spread","someorder":16,"zz":"Available","zz":[{"zz":"1223","zz":"zz 30g","zz":3,"zz":0,"zz":"stale","zz":false,"zz":true,"zz":[],"zz":{"zz":2,"zz":"zz","zz":6,"zzName":"Core zz (zz)","department":56,"zz":"015 zz Spreads","zz":"zz Spread","subzz":100,"somezz":"zz & zz","zz":{},"zz_":100},"zz":{"zz":"2017-09-21T11:09:15.524Z","who":{"zz":"11","zz":"zz"}},"action":{"zz":"Include"},"zz":[{"capturezz":375716,"zz":"Gap","qty":15,"zz":"zz","zz":{"zz":"zz","type":"N","name":"zz","sequence":1},"zz":{"zz":"211","who":{"zz":"11","zz":"zz"}}}]}]}]}]}]}
* def departmentId = getstorewalk.departments[*].id
* print departmentId

其中记录:

19:40:55.135 [main] INFO  com.intuit.karate - [print] ["need to extract this id"]

因此,显然您将响应分配给getstorewalk.

我的猜测是你打算这样做:

def result = callonce read('classpath:zz/zz/zz.feature')
def departmentId = result.response.departments[*].id

请仔细阅读有关调用其他功能文件的文档,您可能会意识到自己做错了什么。

于 2017-09-21T14:08:44.320 回答