{
"code": 200,
"surveyId": 3239640,
"success": 1
}
我想在csv文件中写入SurveyId,但是正在写入null而不是SurveyId。我编写了如下的beanshell脚本:
vars.put ("Response Data",ctx.getPreviousResult().getResponseDataAsString());
log.info(vars.get("Response Data"));
try{
String surveyId = vars.get("surveyId");
}
catch(Exception ex) { log.warn("Error in my script", ex); throw ex; } log.info("surveyId is " +surveyId); f = new FileOutputStream("C://Users//garim//Downloads//apache-jmeter-5.1.1//apache-jmeter-5.1.1//bin//SurveyId.csv", true); p = new PrintStream(f); this.interpreter.setOut(p); print(surveyId); f.close();
您的surveyId
变量未在脚本中的任何位置定义,因此为空,您需要使用JSON Extractor来获取它,并且JSON Extractor必须是before Beanshell PostProcessor
一般来说,since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language用于脚本编写,给定Groovy has built-in JSON support,您可以将代码修改为:]]
String surveyId = (new groovy.json.JsonSlurper().parse(prev.getResponseDataAsString()).surveyId) as String
new File("C://Users//garim//Downloads//apache-jmeter-5.1.1//apache-jmeter-5.1.1//bin//SurveyId.csv") << surveyId << System.getProperty("line.separator")