使用JMeterBeanShell脚本如何从字符串中提取整数

使用JMeterBeanShell脚本如何从字符串中提取整数,jmeter,beanshell,Jmeter,Beanshell,在Jmeter中,我提取了一个JSON字符串 字符串为:{“状态”:“确定”,“错误代码”:0,“消息”:null,“数据”:{“用户计数”:2} 我已经使用JSON路径提取器提取了数据:{“userCount”:2} 现在我需要从{“userCount”:2}中提取'2'。如何使用bean shell脚本实现这一点。我建议您考虑切换到和语言,这样您就可以一箭双雕,而且性能比Beanshell好得多。从父采样器响应中将该2提取为整数的相关代码如下: def json = new groovy.j

在Jmeter中,我提取了一个JSON字符串 字符串为:{“状态”:“确定”,“错误代码”:0,“消息”:null,“数据”:{“用户计数”:2} 我已经使用JSON路径提取器提取了数据:{“userCount”:2}


现在我需要从{“userCount”:2}中提取'2'。如何使用bean shell脚本实现这一点。

我建议您考虑切换到和语言,这样您就可以一箭双雕,而且性能比Beanshell好得多。从父采样器响应中将该
2
提取为整数的相关代码如下:

def json = new groovy.json.JsonSlurper().parseText(prev.getResponseDataAsString())
int userCount = json.data.userCount as int
//do what you need with this "userCount integer"

有关更多详细信息,请参阅文章

在JSON路径提取器中使用$data.userCount作为JSON路径表达式