1: front-facing conditions
Place the Fastjson-1.2.49.jar package in the JMeter Lib directory and add the jar package to the test Plan library; otherwise it will be reported: Typed variable declaration:Class:JSONObject not Found in namespace error
2: Analytic thinking
Use BeanShell to get the JSON response, and then parse the array through Jsonobject and Jsonarray, iterating over the length of the array, extracting the parameter values
We need to parse the JSON response as follows to extract the middle name and population
Import a JSON package
Import org.json.*;
Gets the return value of the GET request
String response_data = prev.getresponsedataasstring ();
Log print Gets the return value of the request
Log.info (Response_data);
Constructs a string-type return value into a Jsonobject object
Jsonobject data_obj = new Jsonobject (response_data);
Gets the parameter value of the post as the next request province (two ways)
String provincelist_str = Data_obj.get ("province"). ToString ();
Jsonarray provincelist_str = Data_obj.getjsonarray ("province");
Log.info (PROVINCELIST_STR);
Gets the length of the province array
int len = Provincelist_str.length ();
String strlen = integer.tostring (len);
Vars.put ("Messagenum", strlen);
Log.info (strlen);
int i = 0;
for (; i < len;++i)
{
Get data[i] Array object
Jsonobject jsontemp = (jsonobject) provincelist_str.getjsonobject (i);
Switch (i)
{
Case 0:
The writing of two kinds of extracting parameters
String Nameitems = jsontemp.getstring ("Name");
String Nameitems = Jsontemp.get ("Name"). ToString ();
Two methods of printing parameters
Vars.put ("Name_1", Jsontemp.getstring ("Name"));
Vars.put ("Name_1", Nameitems);
Log.info (Nameitems);
}
}
After debugging, it was found that two sets of data were extracted, namely the province and the population, which can then be called on subsequent interfaces.
Are you still worried about not finding the interface you can practice? Join Long now and teach you to build your own interface test environment!
Click here to enter the most dynamic test communication platform in history. !
Jmeter (34) _beanshell parse and extract JSON response