数据元:
"mx":[{"batchNum":"000111","goodsItemNumber":"1630655848","orderItemNum":"9210823144249361_1","productCode":"0000035","productNm":"布洛芬混悬液","productUnit":"盒","quantity":10},{"batchNum":"0007","goodsItemNumber":"16306558481","orderItemNum":"9210823144249361_2","productCode":"0000731","productNm":"妇炎洁洗液","productUnit":"盒","quantity":10}]
需要把英文字符 “:” 替换 “=”,逗号之间需要空格
使用正则中的 string.replaceAll 方法替换:
mx.toString().replaceAll("\":\"", "=").replaceAll("\":","=").replaceAll("\",\"",", ").replaceAll("\"","");
替换的结果:
[{batchNum=000111, goodsItemNumber=1630655848, orderItemNum=9210823144249361_1, productCode=0000035, productNm=布洛芬混悬液, productUnit=盒, quantity=10},{batchNum=0007, goodsItemNumber=16306558481, orderItemNum=9210823144249361_2, productCode=0000731, productNm=妇炎洁洗液, productUnit=盒, quantity=10}]
再者需要把花括号"},{" 边界也需要空格
在 replaceAll(String,String) 方法中需要特殊处理英文状态的花括号,在网上找了一下,可以使用下面的写法将英文括号替换成其他字符,(比如中文全角括号):str1.replaceAll("\(","(")。但是在 jmeter BeanShell 这样写 replaceAll("\},","\}, ") 会报如下错误:
2021-08-24 16:00:30,984 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: import org.apache.commons.codec.digest.DigestUtils; import java.util.Date; impo . . . '' Token Parsing Error: Lexical error at line 26, column 68. Encountered: "}" (125), after : "\"\\"
import org.apache.commons.codec.digest.DigestUtils; import java.util.Date; impo . . . '' Token Parsing Error: Lexical error at line 26, column 68. Encountered: "}" (125), after : "\"\"
2021-08-24 16:00:30,984 WARN o.a.j.m.BeanShellPreProcessor: Problem in BeanShell script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of:
最终在 jmeter BeanShell 改写成 replaceAll("},","}, ") 才正常。