{"data": {"device_vec": [
{"agent_version" : "9.7.0.2225","android_id" : "e3d699cf01620531","asset_number" : "","description" : "89vUwdwfVydEjqNAANxM11c72ujdmn","device_name" : "357568061882002","email" : "1504487508089@tt.com","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@tt.com","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}
处理Json数据的方法是使用JMeter的插件,该插件可以使用JSONPath来获取JSON数据中特定位置的数据。类似于XML文件中的XPath,JSONPath可以使用简单的表达式来操作Json对象。JSON Path Extractor是一个开源的增加了post处理器的插件,可以将该插件的Lib文件拷贝到JMeter的lib目录下或者通过Jmeter UI界面的Options-->Plugins Manager下载json插件即可。
$.data.device_vec[1].agent_version
importjava.util.LinkedHashMap;importcom.jayway.jsonpath.JsonPath;importcom.jayway.jsonpath.Predicate;importnet.minidev.json.JSONArray;publicstaticString readjson(String json, String jsonPath) {try{
Object value= JsonPath.read(json, jsonPath,newPredicate[0]);if(valueinstanceofInteger) {returnvalue.toString();
}elseif(valueinstanceofString) {returnvalue.toString();
}elseif(valueinstanceofBoolean){returnvalue.toString();
}elseif(valueinstanceofJSONArray) {
JSONArray arr=(JSONArray)value;if(!arr.isEmpty()){returnarr.toJSONString();}return"";
}elseif(valueinstanceofLinkedHashMap){returnvalue.toString();
}else{returnvalue.toString();
}
}catch(Exception e){return"pathnotfound";
}
}
com.eclipsesource.json,源码下载路径如下 https://github.com/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
本文列举出了三种可用的从Json格式的返回值中提取数据的方法,正则表达式对于简单的Json格式的数据的快速标准化非常占优势。而JsonPath插件可以用于创建能够被维护修改的脚本,但是需要额外的插件安装工作。而最后的带JSON库的BeanShell确实非常详细并且依赖于Java语言的灵活性可以进行进一步的开发。