當(dāng)前位置:首頁(yè) > IT技術(shù) > Web編程 > 正文

jemter beanshell 獲取JSON 對(duì)象中的鍵
2021-10-22 16:47:51

安裝就不多介紹

本文主要講解beanshell 如何將接口返回?cái)?shù)據(jù) 數(shù)據(jù)處理后用于其他接口提供參數(shù) 完成自動(dòng)化測(cè)試閉環(huán)。希望能幫助到大家!后端語(yǔ)言環(huán)境為PHP

獲取JSON 對(duì)象中的鍵

?

?

?獲取上圖中1的步驟如下

1.添加?BeanShell 后置處理程序

import com.alibaba.fastjson.JSON;    
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;//此包沒(méi)有的話 百度一下
//獲取返回?cái)?shù)據(jù) 字符串類型
 String response_data = prev.getResponseDataAsString();
log.info(response_data);
// 字符串轉(zhuǎn)對(duì)象
JSONObject data_obj = JSON.parseObject(response_data);
//獲取到目標(biāo)對(duì)象 {"1":"在線支付","0":"對(duì)公支付"}}
JSONObject jsonObject =  data_obj.get("result").get("data"); 
//聲明數(shù)組變量
  JSONArray jsonArray = new JSONArray();
//遍歷對(duì)象  
for (Map.Entry entry : jsonObject.entrySet()) {
	//添加進(jìn)數(shù)組
	 jsonArray.add(entry.getKey());
    }
    log.info(jsonArray.toString());
    //添加屬性供全線程組使用
     props.put("_expectPayment", jsonArray);

?上步驟存入了數(shù)組變量 ["0","1"] 供其他接口使用

2.在需要使用此數(shù)據(jù)的接口 添加?BeanShell 預(yù)處理程序

import com.alibaba.fastjson.JSON;    
import com.alibaba.fastjson.JSONArray;
//獲取數(shù)組
 JSONArray jsonArray = props.get("_expectPayment");
 //獲取數(shù)組中隨機(jī)值 即獲取到了目標(biāo)值 0或1
     vars.put("expectPayment",jsonArray.get((int)(Math.random()*jsonArray.size())));
      log.info(vars.get("expectPayment"));

 即可通過(guò) ${expectPayment}獲取到0或1 完成自動(dòng)化測(cè)試閉環(huán)。

本文摘自 :https://www.cnblogs.com/

開(kāi)通會(huì)員,享受整站包年服務(wù)立即開(kāi)通 >