首页 > 解决方案 > 在 Jmeter 中提取值

问题描述

我的场景涉及回答一个包含 50 多个问题的测验。点击问题端点会给我一个测验中的所有问题 ID,但每个 ID 至少有 5 个答案 ID,我需要遍历 50 多个问题并为每个问题选择至少一个答案 ID 以提交答案。

目前,我在 Jmet3er 中有以下内容

获取请求 - 获取所有问题 ID 和所有答案 ID

Json Extractor 1 - 存储所有问题 ID

Json Extractor 2 - 仅存储第一个问题的 1 个答案 ID

ForEach 控制器 - 遍历问题 ID 计数器 - n

提交答案请求 - 对于每个问题,但答案相同

请指教

谢谢

标签: jsonperformancejmeter

解决方案


您可以使用JsonPath 过滤器运算符来映射问题的答案

例如,给定这个 JSON:

{
  "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
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  },
  "expensive": 10
} 

您可以通过此 JsonPath 查询拥有所有 4 个作者:

$..author

在此处输入图像描述

但是,如果您想确定The Lord of the Rings书籍的作者,您可以使用前面提到的过滤器表达式,例如:

$..[?(@.title == 'The Lord of the Rings')].author

在此处输入图像描述

更多信息:JMeter 的 JSON 路径提取器插件 - 高级使用场景


推荐阅读