简体   繁体   English

Jmeter-如何读取Json格式的数组形式的GET请求返回的多个值并将其写入CSV文件

[英]Jmeter - How to read multiple values returned by GET request in the form of array in Json format and write it to a CSV file

Jmeter - I have multiple values ( 250 values approx as a minimum ) returned from a Get Request in the form of array in json format , i need pass each of the value in the array as a parameter in next GET request Jmeter-我有一个以json格式的数组形式从Get请求返回的多个值(至少约250个值),我需要在下一个GET请求中将数组中的每个值作为参数传递

Request 1 请求1

> Get http://xxx/store1?

Response in json format looks like this json格式的响应如下所示

{"store1":"peirre","inventorylist":["item1","item2","item3"..........."item250"]}

I will need to use each one of item in next GET request like this to get is feautures like price , available quantity,production site etc., 我将需要像这样在下一个GET请求中使用每一项,以获取价格,可用数量,生产地点等特征,

Request 2 要求2

Get https://xxx/store1/item1?
Get https://xxx/store1/item2?
Get https://xxx/store1/item3?

It would be easy when i can read the response and write each value in the array to CSV file so that my next get request would simply read the CSV file and fire all the requests 当我可以读取响应并将数组中的每个值写入CSV文件时,这将很容易,这样我的下一个get请求将只需读取CSV文件并触发所有请求

Is there a way to achieve this ??? 有没有办法实现这一目标?

Thank you in advance 先感谢您

  1. Add JSON Extractor as a child of the request which returns the above JSON JSON提取器添加为请求的子代,该请求返回上述JSON
  2. Configure it as follows: 如下配置:

    • Names of created variables: anything meaningful, ie item 创建的变量的名称:任何有意义的变量,即item
    • JSON Path Expressions: $.inventorylist.* JSON路径表达式: $.inventorylist.*
    • Match No.: -1 比赛编号: -1
  3. Add ForEach Controller after the first HTTP Request sampler and configure it as follows: 在第一个HTTP Request采样器之后添加ForEach Controller并按以下方式进行配置:

    • Input variable prefix: whatever you used as "Names of created variables" in the JSON Extractor, ie item 输入变量前缀:在JSON提取器中用作“已创建变量的名称”的变量,即item
    • Output variable name: anything meaningful, ie current_item 输出变量名称:任何有意义的变量,即current_item
  4. Add HTTP Request sampler as a child of the ForEach Controller and use https://xxx/store1/${current_item} in "Path" field - it will iterate through all the "item"s HTTP请求采样器添加为ForEach控制器的子代,并在“路径”字段中使用https://xxx/store1/${current_item} -它将迭代所有“项”

    JMeter JSON提取器和ForEach控制器

  1. Add a Json Extarctor to the first Get Request with the following properties 使用以下属性将Json Extarctor添加到第一个Get Request中 在此处输入图片说明

2.Add a JSR223 post processor and initialize a counter as shown below 2.添加一个JSR223后处理器并初始化一个计数器,如下所示 在此处输入图片说明

  1. Add a while controller and place your get request in while controller. 添加一个while控制器,然后将获取请求放入while控制器中。

add the following condition 添加以下条件

${__javaScript(parseInt(vars.get("counter"))<=parseInt(vars.get("List_matchNr")))}

as shown below 如下所示

在此处输入图片说明

  1. To your second get request add a JSR223 post processor and increment the counter as shown below 在第二个获取请求中,添加一个JSR223后处理器并增加计数器,如下所示

    int counter = Integer.parseInt(vars.get("counter")) +1; int计数器= Integer.parseInt(vars.get(“ counter”))+1;

    vars.put("counter",Integer.toString(counter)); vars.put(“ counter”,Integer.toString(counter));

在此处输入图片说明

  1. Use ${__V(List_${counter})} to replace hardcoded value 使用${__V(List_${counter})}替换硬编码值

This loop will run through the match number and sends a request with each item to server 此循环将遍历匹配编号,并将每个项目的请求发送到服务器

在此处输入图片说明

For more information on while loop please follow the link 有关while循环的更多信息,请点击链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM