Jmeter (34) _beanshell parse and extract JSON response

Source: Internet
Author: User
Tags vars

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.