JMeter获取复杂的JSON串中的参数的值

[JMeter获取复杂的JSON串中的参数的值]

大家好,这篇博文中主要是介绍怎么用JMeter的BeanShell去获取复杂的JSON串中的某个参数的值,这将

便于我们用JMeter做出更完美的自动化测试;

首先有这样一个json串:

{

"code": 0,

"data": {

"2018720-0317": [{

"cpurl": "https://cdn.i5sesol.com/isesolImall/",

"cxmc": "",

"jhsl": 1,

"cxwj": "",

"xsddbh": "111807200000645200",

*****      "gybh": "TJTYCPGYBH",

**********      "gybzbh": "20171225000001",
***********      "cpcz": ",,,",

************      "pgdrwbh": "2018720-0317-0366",
*************      "cpmc": "星座+签名定制狮子座",

**************      "bgsl": 0,

***************      "xlh": "2018052401",

****************      "gygxbh": "TJZDXDGX",

*****************      "cpbh": "1532047578068",

******************      "cxList": [],

*******************      "r1": "null"

********************      }],

*********************    "2018720-0318": [{

*********************      "cpurl": "https://cdn.i5sesol.com/isesolImall/",

*********************      "cxmc": "",

*********************      "jhsl": 1,

*********************      "cxwj": "",

*********************      "xsddbh": "111807200000645300",

*********************      "gybh": "TJTYCPGYBH",

*********************      "gybzbh": "20171225000001",

*********************      "cpcz": ",,,",

*********************      "pgdrwbh": "2018720-0318-0367",

*********************      "cpmc": "星座+签名定制狮子座",

*********************      "bgsl": 0,

*********************      "xlh": "2018052401",

*********************      "gygxbh": "TJZDXDGX",

*********************      "cpbh": "1532048096286",

*********************      "cxList": [],

*********************      "r1": "null"

*********************      }]

*********************    },

*********************  "message": "成功"

*********************  }

如上图中,画红框的部分,参数xsddbh(111807200000645300)的值可知,在下一个接口请求中,需要用到上图中的参数

cpbh(1532048096286)的值,然而参数xsddbh在json串中的每个list都有,那么这时候怎么办呢?在这里我将用alibaba.fastjson

这个包进行讲解:

一、下载Alibaba Fastjson Jar包,放在自己能够标识的目录,并在测试计划中引用该jar包,如下图所示:

image

二、在测试计划中添加线程组,并在线程组中添加BeanShell PreProcessor,如下图所示:

image

三、在BeanShell PreProcessor中引入json解析相关jar包,这里用到的jar包如下图所示:
import java.util.Collection;
import java.util.Iterator;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

image

四、将本文开头的json串放到BeanShell PreProcesso,当然需要转移成String类型的,同时并解析该json串

4.1、这里方便大家使用我直接将该String串写出来

String tr = "{"code": 0,"data": {"2018720-0317":[{"r2": "null","cpurl": "https://cdn.i5sesol.com","cxmc":"","jhsl": 1,"cxwj": "","xsddbh": "111807200000645200","gybh": "TJTYCPGYBH","gybzbh": "20171225000001","cpcz": ",,,","pgdrwbh": "2018720-0317-0366","cpmc": "星座+签名定制狮子座","bgsl": 0,"xlh":"2018052401","gygxbh": "TJZDXDGX","cpbh": "1532047578068","cxList": [],"r1": "null"}],"2018720-0318": [{"r2": "null", "cpurl": "https://cdn.i5sesol.com/isesolImall","cxmc": "","jhsl": 1,"cxwj": "","xsddbh": "111807200000645300","gybh": "TJTYCPGYBH","gybzbh": "20171225000001","cpcz": ",,,","pgdrwbh": "2018720-0318-0367","cpmc": "星座+签名定制狮子座","bgsl": 0,"xlh": "2018052401","gygxbh": "TJZDXDGX","cpbh": "1532048096286","cxList": [],"r1": "null"}]},"message": "成功"}";

在BeanShell PreProcessor中显示如下图所示:

image

4.2、将上面的Sting字符串转换成json串,并获取其中data的

//转换成JSON串

  JSONObject json = JSONObject.parseObject(tr);

//获取data的json串;

JSONObject data = json.getJSONObject("data");

4.3、用迭代器去获取data中的元素,并循环

while(it.hasNext()){
Object key = it.next();
String value = JSONObject.toJSONString(key);
JSONArray array = JSONArray.parseArray(value);
System.out.println(value);
for(int i = 0; i < array.size(); i++) {//循环json数组
JSONObject ob = (JSONObject) array.get(i);//得到json对象
System.out.println(ob);
String xsddbh= ob.getString("xsddbh");//获取json对象中列名为xsddbh的值
if(xsddbh.equals("111807200000645300")) {
String cpbh = ob.getString("cpbh");
System.out.println("cpbh"+cpbh);

}
}
}

ps:将上述代码可以在eclipse中调试一下,如果直接粘贴至BeanShell PreProcesso中,可能会有字符串空格问题

4.4、最终解析并获取到cpbh的值在BeanShell PreProcesso中显示如下图所示:

image

五、执行脚本,在控制台中查看cpbh的值,如下图所示:

image

从图中可以看到结果被成功取出

六、如果再希望cpbh这个参数在下面被取到,那么我们需要需要用到jmeter的函数vars.put

vars.put("cpbh",cpbh);

七、查看该参数是否能够传递给下一个http请求,如下图所示:

7.1、添加一个http请求,并引用该变量,如下图所示:

image

7.2、执行脚本,查看变量值是否被引用,如下图所示:

image

至此,jmeter在做自动化是引用比较复杂的变量解析完毕。

这里欢迎大家进群交流:775129837

变化往往都是来的悄无声息,成长不是一蹴而就

你可能感兴趣的:(JMeter获取复杂的JSON串中的参数的值)