如何使用JsonPath访问JSON数组响应?
如何处理REST服务响应作为Json数组返回的情况:例如:如何使用JsonPath访问JSON数组响应?,json,rest,jsonpath,rest-assured,Json,Rest,Jsonpath,Rest Assured,如何处理REST服务响应作为Json数组返回的情况:例如: [{"boolField":true,"intField":991, "StringField":"SampleString"}, {"boolField":false, "intField":998, "StringField": "SampleString2"}]"; 我看到的所有博客和示例都没有涉及上述场景。例如,我检查了表达式语法,但找不到解决方案 例如,下面的json,如果我想获取所有作者,我可以使用$.store.boo
[{"boolField":true,"intField":991, "StringField":"SampleString"},
{"boolField":false, "intField":998, "StringField": "SampleString2"}]";
我看到的所有博客和示例都没有涉及上述场景。例如,我检查了表达式语法,但找不到解决方案
例如,下面的json,如果我想获取所有作者,我可以使用$.store.book[*].author
或$…author
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}]}
}
但是,如果REST服务响应如下所示返回,那么如何从列表中获取所有作者
[
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
]
$…author
将检索所有作者
通常,当根对象是数组时,您可以将
$
看作数组,并执行类似$[0]
的操作来检查第一个项目(但是您仍然会得到一个数组)。在我的情况下[0]。作者工作正常(我省略了$) 我在进行了测试,得到了作者数组,得到了什么结果?java.lang.IllegalArgumentException:使用了参数“author”,但没有定义。当我使用$…author
时,使用JsonPath.params(…)函数定义参数。如果我使用$.author
将返回一个空的[]
。如果我单独使用$
,我将获得该列表。我在jsonpath.curiousconcept.com中进行了尝试,结果出现了未知错误
。我至少能够继续,并且不会被阻止,因为我将得到$
结果作为列表。