如何在Jmeter中解析Json中的数组值



对于下面的响应,我需要获取rideId并将其传递给Jmeter中的下一个请求。另外,生成下面响应的API应该被执行,直到事件类型为HANDSHAKE。

[{"id":90856,"eventType":"HANDSHAKE","parameters":"{"handshakeExpiration":1669217518986,"rideId":3107}"}]

我使用的代码是:

import groovy.json.JsonSlurper;
def jsonSlurper=new JsonSlurper();
def apiDetailsArr=jsonSlurper.parse(prev.getResponseData())
def apiDetails=apiDetailsArr.size()>0?apiDetailsArr.get(0):null
def shouldRun = "1"
if(apiDetails!=null)
{
log.info("details",apiDetails.eventType+"")
if(apiDetails.eventType="HANDSHAKE"){
shouldRun="0"
}
def object=jsonSlurper.parseText(apiDetails.parameters)
log.info("xyz",object+"")
def id=object.rideId;
log.info("id",id+"")
vars.put("id", id+"")
}
else{
shouldRun="1"`enter code here`
}  

`Condition for while controller :     `${__javaScript( "${shouldRun}" != "0",)}``
  1. 所有的log.info函数调用都会导致语法错误,您可以在jmeter.log文件
  2. 中看到它(惊讶!)
  3. 我看不到你在哪里分配shouldRunJMeter变量,这是在While控制器
  4. 中使用的

建议代码修改:

import groovy.json.JsonSlurper;
def jsonSlurper = new JsonSlurper();
def apiDetailsArr = jsonSlurper.parse(prev.getResponseData())
def apiDetails = apiDetailsArr.size() > 0 ? apiDetailsArr.get(0) : null
def shouldRun = "1"
if (apiDetails != null) {
log.info("details", apiDetails.eventType + "")
if (apiDetails.eventType = "HANDSHAKE") {
shouldRun = "0"
}
def object = jsonSlurper.parseText(apiDetails.parameters)
log.info("xyz" + object)
def id = object.rideId;
log.info("id" + id)
vars.put("id", id as String)
} else {
shouldRun = "1"
}  
vars.put("shouldRun", shouldRun)

JMeter: Apache Groovy: What Is Groovy Used For?

相关内容

  • 没有找到相关文章

最新更新