<续>jmeter随笔(12)--定制json响应结果断言
2016-10-30 22:06
537 查看
故事背景
上篇讲了大体两件事情:1、可以写类java脚本定制json分析功能
2、如何制作jmeter的自己的jar包
详细信息,可以看jmeter随笔(12)–定制json响应结果断言
真实需求
jemter随笔(12)中的功能并不能满足我们的实际操作需求。那么我们的真实需求是什么呢?1、获取某个json对象中某个key的value
2、json响应结果中是否存在某个key-value键值对
代码实现
1、获取某个json对象中某个key的value
说明:1.入参abc,json对象的字符串形式;入参theKey,根据此key,获取返回的value
2.返回,找到值为theKey的key-value键值对,返回true,并将value存入对象的字段“theValues”中;反则异之
public boolean getValue(String abc, String theKey) { JSONObject jsonObj = JSON.parseObject(abc); for (Map.Entry<String, Object> entry : jsonObj.entrySet()) { if (entry.getKey().equals(theKey)) { theValues = entry.getValue().toString(); isExitKey = true; // return true; } else { if ("JSONArray".equals(entry.getValue().getClass() .getSimpleName())) { JSONArray jsonArray = JSON.parseArray(String.valueOf(entry .getValue())); for (int i = 0; i < jsonArray.size(); i++) { getValue(String.valueOf(jsonArray.get(i)), theKey); } } else if ("JSONObject".equals(entry.getValue().getClass() .getSimpleName())) { getValue(entry.getValue().toString(), theKey); } else { continue; } } } return isExitKey; }
编写此处代码,踩了个坑-递归的结束。代码中已经被我填平。
2、json响应结果中是否存在某个key-value键值对
说明:1.入参abc,json对象的字符串形式;入参theKey和expectValue,匹配这对key-value是否存在
2.返回,找到值为theKey的key-value键值对,返回true;反则异之
public boolean checkIsExitTheValue(String abc, String theKey, String expectValue) { JSONObject jsonObj = JSON.parseObject(abc); for (Map.Entry<String, Object> entry : jsonObj.entrySet()) { if (entry.getKey().equals(theKey)) { if (expectValue.equals(entry.getValue().toString())) { isExitTheValue = true; } } else { if ("JSONArray".equals(entry.getValue().getClass() .getSimpleName())) { JSONArray jsonArray = JSON.parseArray(String.valueOf(entry .getValue())); for (int i = 0; i < jsonArray.size(); i++) { checkIsExitTheValue(String.valueOf(jsonArray.get(i)), theKey, expectValue); } } else if ("JSONObject".equals(entry.getValue().getClass() .getSimpleName())) { checkIsExitTheValue(entry.getValue().toString(), theKey, expectValue); } else { continue; } } } return isExitTheValue; }
踩的坑
在写递归方法的代码段时,我以为用return可以结束嵌套的多层递归。(我天真了)。下面代码中注释掉的return语句,替换成了赋值语句,才解决问题(出于代码效率考虑的话,等于true后,还是会跳出待外层,继续执行方法。)
if (entry.getKey().equals(theKey)) { theValues = entry.getValue().toString(); isExitKey = true; // return true; }
补充:网上搜了一些,通常使用抛出异常的方法,来结束递归。
源代码
上面方法的源代码可以打成jar包,放入jmeter,可以直接使用。
相关文章推荐
- jmeter随笔(12)--定制json响应结果断言
- jmeter插件之jsonpath提取响应结果和做断言
- JMeter 测试过程中的响应断言以及断言结果
- Jmeter 接口测试中使用Beanshell断言: 将接口响应报文与数据库结果对比
- <续>jmeter随笔(22)将测试结果同步写入数据库更新包
- Jmeter入门实战(二)如何使用Jmeter的BeanShell断言,把响应数据中的JSON跟数据库中的记录对比
- 2016-12-03(两种处理JSON返回页面的方式、分页,ModelAndView和Model的区别,<C:forEach>注意)
- jmeter 通过csv data set config 设置参数化后,执行结果显示为<EOF>
- 使用jmeter做接口测试,怎么验证、核对响应结果中json的内容?
- 将DataTable或Ilist<>转换成JSON格式
- 将IList<T>泛型转换成Json
- 在JavaWEB中将Ajax动态查询结果转换成List<Map>型JavaScript数组
- <转>Android 2.2 r1 API 中文文档系列(12) —— Button
- MSP430程序库<八>DAC12的使用
- C++之乱七八糟<真正的随笔>
- 将DataTable或Ilist<>转换成JSON格式
- MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)<转>
- 实体或List(IList<T>)转换JSON格式的简便方法
- 想搞死linux的疯狂代码<结果还是失败了>
- JQuery将DataTable list<>数据转换成JSON数据 动态创建表格显示数据