在之前写过提取json数据格式的文章,这次对jmeter读取json数据格式进行整理。
举例一个接口的response 格式如下:
{"data" : {"device_vec" : [{"agent_version" : "9.7.0.2225","android_id" : "e3d699cf01620531","asset_number" : "","description" : "89vUwdwfVydEjqNAANxM11c72ujdmn","device_name" : "357568061882002","email" : "1504487508089@","encryption_log_count" : 0,"encryption_version" : "","engine_version" : "9.715-1024","firewall_log_count" : 0,"firewall_version" : "","group_id" : "aa000000-0000-0000-0000-000000000000","group_name" : "Default","id" : "35756806-1882-0020-0000-000000000000","imei" : "357568061882002","inactive_reason" : 0,"install_time" : 1503642124,"last_connected_time" : 1504742375,"last_scan_time" : 1503642674,"meid" : "","noncomp_reason" : "","os_version" : "4.3","pattern_version" : "2.437.00","phone_number" : "","platform_type" : 512,"policy_id" : 32,"policy_name" : "","security_status" : 3,"status" : 3,"user_name" : "test1504487508089","virus_log_count" : 26,"wtp_log_count" : 0},{"agent_version" : "2.0.0.1518","android_id" : "","asset_number" : "","description" : "3dLAbTlj7UQoOiodnAjDrfX1iI0PCx","device_name" : "Michael’s iPhone","email" : "1504487508089@","encryption_log_count" : 0,"encryption_version" : "","engine_version" : "","firewall_log_count" : 0,"firewall_version" : "","group_id" : "aa000000-0000-0000-0000-000000000000","group_name" : "Default","id" : "6954500b4f14e50bd20634481ee2c6d9f17b4ee3","imei" : "35 445006 267069 9","inactive_reason" : 0,"install_time" : 1503641446,"last_connected_time" : 1503652862,"last_scan_time" : 1503641477,"meid" : "35445006267069","noncomp_reason" : "","os_version" : "10.3.2","pattern_version" : "","phone_number" : "","platform_type" : 1024,"policy_id" : 6,"policy_name" : "","security_status" : 1,"status" : 3,"user_name" : "test1504487508089","virus_log_count" : 0,"wtp_log_count" : 0}],"total_count" : 2},"error_code" : 1,"message" : "Success","timestamp" : 1504765848}
下面就对device_vec取角标2的agent_version 参数进行提取。
方法一:Jmter Json插件,JSON Path Extractor 提取器。
处理Json数据的方法是使用JMeter的插件,该插件可以使用JSONPath来获取JSON数据中特定位置的数据。类似于XML文件中的XPath,JSONPath可以使用简单的表达式来操作Json对象。JSON Path Extractor是一个开源的增加了post处理器的插件,可以将该插件的Lib文件拷贝到JMeter的lib目录下或者通过Jmeter UI界面的Options-->Plugins Manager下载json插件即可。
可以使用如下的JSONPath进行描述:
$.data.device_vec[1].agent_version
在JMeter中,只需要从PostProcessor菜单中打开JSON Path Extractor然后输入变量名与默认值即可,如下所示:
JSONPath表达式较短并且易于阅读,能够有效提高测试脚本的易维护性,该插件并不随着标准的JMeter一起安装。
方法二:Jmter 正则表达式插件,正则表达式提取
JMeter安装了正则表达式插件之后,可以按照固定的格式从字符串中提取数据,而本例中正则表达式如下所示:
"agent_version":"(.+?)"
使用了该表达式之后会返回所有服从表达式的字符串,但是只有一个表达式是我们所关注的。可以使用1个组作为模板($1$),而2则会返回第二个数据。
方法三:Jmter BeanShell提取器,BeanShell提取器
借用了JMeter的对于BeanShell支持的特性,BeanShell是一个轻量级的面向Java的脚本语言。BeanShell Post Processor允许使用标准的Java语法来处理Json数据,使用方法如下图所示:
1.写一个jsonpath读取代码如下:
import java.util.LinkedHashMap;import com.jayway.jsonpath.JsonPath;import com.jayway.jsonpath.Predicate;import net.minidev.json.JSONArray;public class GetJson {public static String readjson(String json, String jsonPath){try{Object value = JsonPath.read(json, jsonPath, new Predicate[0]);if (value instanceof Integer){return value.toString();}else if (value instanceof String){return value.toString();}else if (value instanceof Boolean){return value.toString();}else if (value instanceof JSONArray){JSONArray arr = (JSONArray)value;if(!arr.isEmpty()){return arr.toJSONString();}elsereturn "";}else if (value instanceof LinkedHashMap){return value.toString();}else if (value instanceof Float) {return value.toString();}else{return value.toString();}}catch(Exception e){return "pathnotfound";}}}
然后使用此方法来读取json数据格式
2.导入com.eclipsesource.json 方法直接读取json
com.eclipsesource.json,源码下载路径如下 /ralfstx/minimal-json,只需将源码打成jar包放到jmeter的lib/ext目录下即可
com.eclipsesource.json 适用方法:http://static.javadoc.io/com.eclipsesource.minimal-json/minimal-json/0.9.3/com/eclipsesource/json/JsonObject.html
3.如果是循环读取,那就要算出List的长度做循环---用jsonpath读取方式去循环取(前面的jsonpath方法不再贴)
4.如果是循环读取,那就要算出List的长度做循环---用com.eclipsesource.jsonapi 读取方式循环读
如下: