Hello everyone, this blog post is mainly about how to use JMeter BeanShell to get the value of a parameter in a complex JSON string, which will
It is convenient for us to use JMeter to make more perfect automation test;
First there is such a JSON string, as shown in:
In the case of a red-framed part, the value of parameter XSDDBH (111807200000645300) indicates that the parameters in the next interface request need to be used
The value of CPBH (1532048096286), while the parameter xsddbh is in every list in the JSON string, what happens then? I'm going to use Alibaba.fastjson here.
This package is explained:
First, download the Alibaba Fastjson jar package, put it in a directory that you can identify, and reference the jar package in your test plan, as shown in:
Second, add the thread group in the test plan and add BeanShell preprocessor to the threads group as shown in:
In BeanShell preprocessor, the JSON parsing related jar package is introduced, and the jar package used here is as follows:
To put the JSON string at the beginning of this article into BeanShell Preprocesso, of course, it needs to be transferred to a string type, and parse the JSON string
4.1, it is convenient for everyone to use I write the string directly
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\ ": \" Constellation + Signature custom Leo \ ", \" 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\": \ "Constellation + Signature custom Leo \", \ "bgsl\": 0,\ "xlh\": \ "2018052401\", \ "gygxbh\": \ "Tjzdxdgx\", \ "cpbh\": \ "1532048096286\", \ "cxlist\": [],\ "r1\": \ "null\"}]},\ "message\": \ "success \"} ";
Displayed in BeanShell preprocessor as shown in:
4.2. Convert the sting string above into a JSON string and get the data
Convert to JSON string
Jsonobject json = jsonobject.parseobject (TR);
Gets the JSON string for data;
Jsonobject data = json1.getjsonobject ("Data");
4.3. Use an iterator to get the elements in data and loop
Returns a iterator using the container's iterator () method
Collection iterator = Data.values ();
Iterator it = Iterator.iterator ();
Hasnext () method to determine if there are elements in the container
while (It.hasnext ()) {
Returns the first element through the next () method of iterator
Objectkey = It.next ();
Gets the value values below the first element
String value = jsonobject.tojsonstring (key);
Converts the value below the element to JSON
Jsonarray array = Jsonarray.parsearray (value);
System.out.println (value);
for (int i = 0; i < array.size (); i++) {//Loop JSON array
Jsonobject ob = (jsonobject) array.get (i);//Get JSON Object
String xsddbh= ob.getstring ("XSDDBH");//Gets the value of the column named Xsddbh in the JSON object
if (Xsddbh.equals ("111807200000645300")) {
String CPBH = ob.getstring ("CPBH");
System.out.println ("The value is" +CPBH);
}
}
}
4.4. The final parse and get to CPBH value is shown in BeanShell Preprocesso as shown in:
Execute the script and view the value of the CPBH in the console, as shown in:
You can see that the results were successfully removed.
Six, if you want to cpbh this parameter is taken below, then we need to use the JMeter function vars.put
Vars.put ("CPBH", CPBH);
Seven, see if the parameter can be passed to the next HTTP request, as shown in:
7.1. Add an HTTP request and reference the variable as shown in:
7.2. Execute the script to see if the value of the variable is referenced, as shown in:
At this point, JMeter is doing automation is to refer to the more complex resolution of the variable.
You are welcome to join us in group communication: 775129837
JMeter using BeanShell to get the value of a parameter in a complex JSON string