如何在jmeter中替换字符串中的变量?

ulydmbyx  于 2023-11-19  发布在  其他
关注(0)|答案(1)|浏览(143)

我正在使用Beanshell预处理器,我需要将值替换为我的“String”来分配参数值(JSON格式),我有这个

String textToSign = 
"{ \"origin\": \"${origin_encrypted}\"," +
"  \"destiny\": \"${destiny_encrypted}\"," +
"  \"subject\": \"${subject_encrypted}\"," +
"  \"amount\": \"${amount_encrypted}\"," +
"  \"softToken\": \"${softToken_encrypted}\" }";
log.info("Text To Sign: " + textToSign);

字符串
但是我得到了Text To Sign: { "origin": "${origin_encrypted}", "destiny": "${destiny_encrypted}", "subject": "${subject_encrypted}", "amount": "${amount_encrypted}", "softToken": "${softToken_encrypted}" },请帮助我:(

rm5edbpk

rm5edbpk1#

首先,您应该重新考虑使用Beanshell,因为JMeter 3.1建议使用JSR223 Test Elements和Groovy语言进行脚本编写。
Groovy还提供more handy way to build JSON而不是字符串连接。

def payload = [:]

payload.put('origin', vars.get('origin_encrypted'))
payload.put('destiny', vars.get('destiny_encrypted'))
payload.put('subject', vars.get('subject_encrypted'))
payload.put('amount', vars.get('amount_encrypted'))
payload.put('softToken', vars.get('softToken_encrypted'))

String textToSign = new groovy.json.JsonBuilder(payload).toPrettyString()

log.info("Text To Sign: " + textToSign)

字符串
关于JMeter中Groovy脚本的更多信息:Apache Groovy: What Is Groovy Used For?

相关问题