Gatling/Scala中JSONPath无法捕获指定字段问题求助
Gatling中正确获取JSON数组指定元素display字段的方法
给定如下JSON响应体:
[ { "system": "http://snomed.info/sct", "code": "735938006", "display": "akutt hodepine" }, { "system": "http://snomed.info/sct", "code": "267096005", "display": "frontal hodepine" }, { "system": "http://snomed.info/sct", "code": "103011009", "display": "godartet anstrengelseshodepine" }, { "system": "http://snomed.info/sct", "code": "25064002", "display": "hodepine" }, { "system": "http://snomed.info/sct", "code": "38823002", "display": "hodepine med aura" }, { "system": "http://snomed.info/sct", "code": "193031009", "display": "klasehodepine" }, { "system": "http://snomed.info/sct", "code": "230465000", "display": "migrene med aura uten hodepine" }, { "system": "http://snomed.info/sct", "code": "330007", "display": "oksipital hodepine" }, { "system": "http://snomed.info/sct", "code": "54012000", "display": "posttraumatisk hodepine" }, { "system": "http://snomed.info/sct", "code": "4969004", "display": "sinushodepine" }, { "system": "http://snomed.info/sct", "code": "398057008", "display": "tensjonshodepine" }, { "system": "http://snomed.info/sct", "code": "128187005", "display": "vaskulær hodepine" } ]
需要获取数组中第4个元素(索引为3)的display字段值hodepine,但使用IntelliJ生成的路径转换为Gatling代码后执行失败:
.check(jsonPath("$../3/display").saveAs("display"))
报错信息:
> jsonPath($../3/display).find.exists, found nothing
解决方法
- 问题根源:IntelliJ的「Copy JSON Pointer」遵循RFC 6901标准格式,而Gatling的
jsonPath依赖Jayway JsonPath语法,两者格式不兼容。 - 正确写法:使用Jayway的数组索引语法定位元素,修改后的代码如下:
.check(jsonPath("$[3].display").saveAs("display"))
也可以写成$.[3].display(数组前的.为可选语法),两种写法都能正确定位到目标字段。
内容的提问来源于stack exchange,提问作者Magnus Jensen
相关产品推荐
相关产品推荐

