Jmeter_Beanshell parses and extracts the response json

1: pre-conditions

The fastjson-1.2.49.jar package placed in the lib directory jmeter and add the jar package to the Library test plan; otherwise it will be reported: Typed variable declaration: Class: JSONObject not found in namespace error

2: analytical thinking

After beanshell acquired using json response, and then analyzed by the array and JSONObject JSONArray, length through the array, the parameter values ​​extracted

We need to parse the following json response, and extracted middle population Name

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
//import java.util.HashMap;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;
import java.util.List;

//获取获取请求的返回值
String response_data = prev.getResponseDataAsString();
//日志打印获取请求的返回值
log.info(response_data);
//将String类型的返回值构造成JSONObject对象
//JSONObject data_obj = JSONObject.parseObject(response_data);
JSONObject data_obj= JSON.parseObject(response_data);  
log.info("--------------" + data_obj.toString());

//获取作为下一个请求post的参数值Province(两种方式)
String Provincelist_str = data_obj.getString("data");
log.info(Provincelist_str);
JSONObject jsondata= JSON.parseObject(Provincelist_str);
log.info("--------------" +jsondata.toString());

String CUUID = JSONPath.eval(data_obj,"$.data.elements[0].courses[0].courseUUID");
log.info(CUUID);
vars.put("MessageNum",CUUID);

Guess you like

Origin www.cnblogs.com/v-hlyu/p/v_hlyu.html