问题
In my jmeter response i have javascript and json as a mix response from that i have to fetch a JSON array which is
items
Here is part of a huge response which i am getting
DE.APP.task.TaskGrid.getAssignmentData = function () {
return {"items":[]};
};
DE.APP.task.TaskGrid.getResourceData = function () {
return {"items":[{"STANDARDRATEFORMAT":"0.00","ASSIGNED_HRS":0,"RESOURCE_NAME":"#Buddhika ","COST":"0.00","PERCENTASSIGNED":"100.00","EMAIL":"Buddhika75@mspblank.com","AVAILABLEFROM":"10-May-2011","ALLOCATED_HRS":"1872.00","RESOURCE_ID":36197221,"AVAILABLETO":"31-Mar-2012","calendar":{"exceptions":{},"weekDayHours"
In this response i have to fetch the json array which is in getResourceData() and not any other items. How can we do this with beanshell assrtion?
回答1:
You can use apache StringUtils to cut the string using a left boundary and a right boundary. Your left boundary is clear "return {" but I am not sure about your right boundary (the string that follows your JSON payload). In the below example, I've used the ]} as the delimiter to cut your JSON payload. Use prev.getResponseDataAsString() to pull the response payload as a string.
import org.apache.commons.lang3.StringUtils;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONArray;
String sInputString = "return {\"items\":[{\"STANDARDRATEFORMAT\":\"0.00\",\"ASSIGNED_HRS\":0,\"RESOURCE_NAME\":\"#Buddhika \",\"COST\":\"0.00\",\"PERCENTASSIGNED\":\"100.00\",\"EMAIL\":\"Buddhika75@mspblank.com\",\"AVAILABLEFROM\":\"10-May-2011\",\"ALLOCATED_HRS\":\"1872.00\",\"RESOURCE_ID\":36197221,\"AVAILABLETO\":\"31-Mar-2012\",\"calendar\":{\"exceptions\":{},\"weekDayHours\":{}}}]}";
//String sInputString = prev.getResponseDataAsString();
try {
// Use StringUtils to cut the string between the two
String sCutString = StringUtils.substringBetween(sInputString, "return {\"items", "]}");
String sFinalString = "{\"items" + sCutString + "]}";
log.info("sFinalString=" + sFinalString);
// Use JSONParser to parse the JSON
JSONParser parser = new JSONParser(JSONParser.ACCEPT_NON_QUOTE|JSONParser.ACCEPT_SIMPLE_QUOTE);
JSONObject rootObject = (JSONObject) parser.parse(sFinalString);
//JSONObject rootObject = (JSONObject) parser.parse(prev.getResponseDataAsString());
JSONArray jResourceArray = (JSONArray) rootObject.get("items");
for (int i=0; i < jResourceArray.size(); i++) {
log.info(jResourceArray.get(i).toString());
// You can access individual elements using this
log.info("RESOURCE_ID=" + jResourceArray.get(i).get("RESOURCE_ID"));
}
}
catch ( Exception ex) {
log.info("Exception.." + ex);
}
回答2:
You can extract any part of the response using Regular Expressions. As action is need to perform on the response of particular request[sampler], you should use Post-Processors as child of that particular request. So basically use Regular Expression Extractor.
Assuming that your response is like this---
I have completed array of JSON object and added some tell which may be there. This is assumption that I'm making. Please check your actual response.
DE.APP.task.TaskGrid.getAssignmentData = function () {
return {"items":[]};
};
DE.APP.task.TaskGrid.getResourceData = function () {
return {"items":[{"STANDARDRATEFORMAT":"0.00","ASSIGNED_HRS":0,"RESOURCE_NAME":"#Buddhika ","COST":"0.00","PERCENTASSIGNED":"100.00","EMAIL":"Buddhika75@mspblank.com","AVAILABLEFROM":"10-May-2011","ALLOCATED_HRS":"1872.00","RESOURCE_ID":36197221,"AVAILABLETO":"31-Mar-2012","calendar":{"exceptions":{},"weekDayHours":{}}}]}
AnotherBlockthatYouCanIDenity
So you can have Post-Processor like this-
Please note that this RE may not be correct as i'm not sure about the response you are getting. Please check your response. You can test your regular expression in View Results Tree listener also.
来源:https://stackoverflow.com/questions/42853956/fetching-json-response-in-jmeter-using-beanshell