大家好,這篇博文中主要是介紹怎么用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包,如下圖所示:
二、在測試計划中添加線程組,並在線程組中添加BeanShell PreProcessor,如下圖所示:
三、在BeanShell PreProcessor中引入json解析相關jar包,這里用到的jar包如下圖所示:
四、將本文開頭的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中顯示如下圖所示:
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中顯示如下圖所示:
五、執行腳本,在控制台中查看cpbh的值,如下圖所示:
從圖中可以看到結果被成功取出
六、如果再希望cpbh這個參數在下面被取到,那么我們需要需要用到jmeter的函數vars.put
vars.put("cpbh",cpbh);
七、查看該參數是否能夠傳遞給下一個http請求,如下圖所示:
7.1、添加一個http請求,並引用該變量,如下圖所示:
7.2、執行腳本,查看變量值是否被引用,如下圖所示:
至此,jmeter在做自動化是引用比較復雜的變量解析完畢。
這里歡迎大家進群交流:775129837