【问题标题】:how can I get more than one element from json using jsonpath?如何使用 jsonpath 从 json 中获取多个元素?
【发布时间】:2018-03-11 09:26:54
【问题描述】:

我有一个简单的 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 路径断言。

【问题讨论】:

    标签: jmeter jsonpath


    【解决方案1】:

    请参阅Dmitri T answer 了解 JSON 路径断言插件。

    因此,使用 Core JMeter,您可以使用 JSON ExtractorResponse Assertion 使用范围“JMeter Variable”:

    【讨论】:

    • @The nice downvoters,你能评论一下你为什么投反对票吗?我仍然爱你:-)
    【解决方案2】:

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

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

    参考资料:

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-02-01
        • 2014-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-12
        • 1970-01-01
        相关资源
        最近更新 更多