jmeter响应信息unicode 编码转成中文

时间:2022-09-19 22:48:44

在jmeter 发送请求过程中,有时候后台返回的是unicode 代码,如:

{"status":-1,"msg":"\u63d0\u4ea4\u6570\u636e\u4e0d\u8db3"}

jmeter响应信息unicode 编码转成中文


手动转换成中文为:

{"status":-1,"msg":"提交数据不足"}

需要使用jmeter 把响应内容转换成中文显示,方便查看。思路是使用bean shell 把unicode响应结果转换成中文,步骤为:

1、右键点击请求,添加后置处理器,BeanShell PostProcessor

jmeter响应信息unicode 编码转成中文

2、在BeanShell PostProcessor(bean shell脚本语法和用法和java一致)设置脚本为:

private static String ascii2native ( String asciicode )
{
String[] asciis = asciicode.split ("\\\\u");
String nativeValue = asciis[0];
try
{
for ( int i = 1; i < asciis.length; i++ )
{
String code = asciis[i];
nativeValue += (char) Integer.parseInt (code.substring (0, 4), 16);
if (code.length () > 4)
{
nativeValue += code.substring (4, code.length ());
}
}
}
catch (NumberFormatException e)
{
return asciicode;
}
return nativeValue;
}
String asciicode =new String(prev.getResponseData(),"UTF-8");
prev.setResponseData(ascii2native(asciicode));

jmeter响应信息unicode 编码转成中文

3、执行查看响应信息,响应信息转换为中文:

jmeter响应信息unicode 编码转成中文

注意:如果还不能转换成中文,需要把jmeter中的配置文件jmeter.properties的配置项sampleresult.default.encoding 修改为utf-8,如不存在这一配置项就添加一行

sampleresult.default.encoding=utf-8

再重新启动jmeter、执行,就可以显示为中文了