groovy - Jmeter Groovy如何用{替换这个字符串
问题描述
在带有 JSR223 Groovy 的 Jmeter 中。我花了很多时间尝试在 JSON 块中替换这个字符串
"ABC": {"seconds": 20},
和
"ABC": {"seconds": ${myVal}}, (this way my value in seconds is variable)
我努力了
str1 = str1.replaceAll('"ABC": {"seconds": 20}', '"ABC": {"seconds": '+${myVal}+'"}"');
但它不会工作。请帮忙,
解决方案
考虑:
def s = '''
{"ABC": {"seconds": 20},
"DEF": {"seconds": 30},
"IJK": {"seconds": 40}}
'''
def myVal = 88
def oldRegex = /"ABC": \{"seconds": 20\}/
def newStr = '"ABC": {"seconds": ' + myVal + '}'
def s2 = s.replaceAll(oldRegex, newStr);
println s2
注意第一个参数replaceAll
是一个正则表达式,这意味着{
并且}
必须被转义。在 Groovy 中转义字符时,我们通常更喜欢/\{/
(a slashy-string) 而不是"\\{"
.
推荐阅读
- json - MappedListIterable
' 不是类型 'List 的子类型 ' - msbuild - 如何通过 MSBuild 复制任务复制 NuGet 包引用的 contentFiles
- primefaces - 如何在primefaces的列标签中隐藏某些列?
- python - buildozer APK 无法在 Android 手机上启动
- git - VSCode Git 无法正确显示文件中的提交
- python - discord.py 自定义帮助信息
- python - 从子类访问存储在父类中的 DataFrame
- spring-integration - 将 EmbeddedKafkaBroker 与 spring 集成和 spring kafka 一起使用
- ruby-on-rails - 如何修复 Rails 导出的 Excel (.xls) 文件中的字符编码?
- performance - random_number 子程序运行时间比较 ifort 与 gfortran