如何使用 jsonpath 从 json 获取多个元素?

how can I get more than one element from json using jsonpath?

我有一个简单的 JSON,我想获得两个属性:

corematching 并验证 core : truematching : 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 路径断言。

有关 JSON 路径断言插件,请参阅

因此,使用 Core JMeter,您可以使用范围为“JMeter Variable”的 JSON Extractor and Response Assertion 这样做:

您应该能够使用 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;
}

按如下方式配置您的 JSON 路径断言:

  • JSON 路径:[?($.core == true && $.matching == true)]
  • 勾选Validate against expected value
  • 期望值:[]
  • 勾选Invert Assertion方框

参考文献: