如何从json中使用jsonpath获取多个元素?
问题描述:
我有一个简单的JSON,我想获得两个属性:如何从json中使用jsonpath获取多个元素?
core
和matching
并验证是否core : true
和matching : true
。
{
"lockVersion"
: 1,
"updatedBy" : "jan",
"updatedOn" : "2016-09-25T11:21:45Z",
"id" : 964,
"title" : "Corporate Numeric",
"description" : null,
"descType" : 31084140,
"descValueType" : 31084136,
"defaultSourceOfVerification" : "Source",
"core" : true,
"matching" : true,
"anything" :
[
],
"authorized"
:
[
1
]
}
是否有可能使用AND运算符执行此操作,还是必须执行两步操作来提取一个集合,然后再次筛选以获得最终结果? 我打算使用jp @ gc - JSON Path Assertion。
答
配置您的JSON路径声明如下:
- JSON路径:
[?($.core == true && $.matching == true)]
- 蜱
Validate against expected value
箱 - 期望值:
[]
- 蜱
Invert Assertion
箱
参考文献:
答
请参阅Dmitri T answer JSON路径断言插件。
所以使用核心JMeter的,你可以使用JSON Extractor和Response Assertion与范围做这样的 “JMeter的变量”:
+0
@The不错downvoters,可你把你为什么downvoted评论?我仍然爱你:-) –
答
您应该能够使用BeanShell断言和内置的JSON解析器来实现此目的。
import net.minidev.json.parser.JSONParser;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONArray;
try {
// Use JSONParser to parse the JSON
JSONParser parser = new JSONParser(JSONParser.ACCEPT_NON_QUOTE|JSONParser.ACCEPT_SIMPLE_QUOTE);
JSONObject rootObject = (JSONObject) parser.parse(prev.getResponseDataAsString());
if (rootObject.get("matching") && rootObject.get("core")) {
Failure=false;
}
else {
Failure=true;
}
}
catch (Exception ex) {
log.info("Exception.." + ex);
Failure=true;
}
我upvoted你的答案,谢谢 –
这个解决方案适合我,谢谢;-) – Kac