首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Jmeter如何将此字符串替换为{

Jmeter如何将此字符串替换为{
EN

Stack Overflow用户
提问于 2018-05-29 07:07:15
回答 1查看 1.7K关注 0票数 0

在Jmeter中使用JSR223 Groovy..我花了很多时间在JSON块中替换这个字符串

"ABC": {"seconds": 20}, 

使用

"ABC": {"seconds": ${myVal}}, (this way my value in seconds is variable)

我试过了

str1 = str1.replaceAll('"ABC": {"seconds": 20}', '"ABC": {"seconds": '+${myVal}+'"}"');

但这行不通。请帮帮我

EN

回答 1

Stack Overflow用户

发布于 2018-05-29 08:00:13

考虑一下:

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中转义字符时,我们通常更喜欢/\{/ (斜杠字符串)而不是"\\{"

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50574355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档