Jmeter4.0 _Beanshell json parse and acquire response data array length

       When we do jmeter interface test of time, and sometimes did not return data across development total, only to return a record, but then, we need to know in the end how many returned data, we supposed to do? I supposed to do? I supposed to do?

      Do not worry, then, let us witness how the miracle is born! ! !

  Some people may see a lot of online data parsing json return the article, looks around are the same, Ever since, I pro-test a little, really useless! ! ! (It may be too technical slag, can not comprehend the essence of them) no recourse but to try to see the log information time and time again slightly! Finally doing a good job with the help of various chiefs!

The first step: the fastjson-1.2.62.jar package placed in the lib directory jmeter, and the jar package is added to the Library test plan; otherwise it will be reported: Typed variable declaration: Class: JSONObject not found in namespace of error (insurance purposes, then fastjson-1.2.62.jar pack placed under the same lib / ext directory jmeter, the same package is added to the jar test program in the Library)

  

 

 We look at the data format to be parsed

 

 We want to get to the item array length, that is, how many pieces of data under item.

Step two: we want to get to that data, interfaces to a request to obtain data about, then in the interface request plus a post-processor BeanShell PostProcessor. Man of few words said, the code line and up! (Interface to do your own one, I will not put up)

com.alibaba.fastjson amount. *;

// the return value of the acquisition request
String RESPONSE_DATA prev.getResponseDataAsString = ();
// the return value of the log acquisition request print
log.info ( "============== response_data ==" + response_data + "=========================");
// the return value of type String configured JSONObject objects
JSONObject = data_obj the JSON.parse (RESPONSE_DATA) ;
// print log value data_obj
log.info ( "============== data_obj ==" + data_obj + "================= ======== ");

// turn into the data type JSONObject
JSONObject jsonData = data_obj.getJSONObject ( "data" );

// Log Print jsonData value
log.info ( "============== jsonData ==" + jsonData + "================== ======= ");

// Get the next parameter as a post value requested item (Get string)
// String = jsonData.get Item ( "Item") toString ().;

// Get the next parameter as a post value requested item (Get the JSONArray)
the JSONArray Item = jsonData.getJSONArray ( "Item");

log.info("==============item=="+item+"=========================");

// Get the length of the item string
// int len = item.length ();

// Get length of array item
int len = item.size ();

String itemLen = Integer.toString(len);

log.info("==============itemLen=="+itemLen+"=========================");
vars.put("itemNum",itemLen);

 

The above code so BeanShell PostProcessor stuffed into. Dear viewers, seeing no, I was almost every line of a Log, me too hard.

The third step: the implementation of what, if you are lucky you will see jmeter log print out the following log information

 

 By the way, the last line of code vars.put ( "itemNum", itemLen); this is a itemNum variable to plug in jmeter convenient to call back. As a result, we get the data we want.

 

Plan Key: BeanShell PostProcessor why it has marked red? Because many online all-is written: JSONObject data_obj = new JSONObject (response_data ); the results I have tried this, of course, I failed, incorrect report, Can not find constructor: com.alibaba.fastjson.JSONObject ( java. lang.String) in class :, then I look through json source code, to see two constructors: public JSONObject () and JSONObject public (boolean isNull ), it simply has not JSONObject public (String  text ) such a constructor, not Le strange error! Another way conversion JSONObject objects found JSON.parse (String text); this perfect solution. 

 

 

  

 

Guess you like

Origin www.cnblogs.com/zzxblogs/p/11866188.html