source("D:\\apache-jmeter-3.0\\用例\\Test.java");
public static void f(){
String response_data=prev.getResponseDataAsString();
String JsonPath="$.data.groupList[0].dateList";
String result=Test.readjson(response_data,JsonPath);//调用方法时语句要在方法体内,不能直接写语句,否则报错。
log.info("---------------"+result+"---------------");
}
f();
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Predicate;
import net.minidev.json.JSONArray;
public class Test {
public static String readjson(String json, String jsonPath) {
try
{
Object value = JsonPath.read(json, jsonPath, new Predicate[0]);
if (value instanceof Integer)
{
return value.toString();
}else if (value instanceof String)
{
return value.toString();
}else if (value instanceof Boolean)
{
return value.toString();
}else if (value instanceof JSONArray)
{
JSONArray arr = (JSONArray)value;
if(!arr.isEmpty()){return arr.toJSONString();}
return "";
}else if (value instanceof LinkedHashMap)
{
return value.toString();
}else if (value instanceof float)
{
return value.toString();
}else{
return value.toString();
}
}
catch(Exception e){
return "pathnotfound";
}
}
}
Test.java
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Predicate;
import net.minidev.json.JSONArray;
public class Test {
public static String readjson(String json, String jsonPath) {
try
{
Object value = JsonPath.read(json, jsonPath, new Predicate[0]);
if (value instanceof Integer)
{
return value.toString();
}else if (value instanceof String)
{
return value.toString();
}else if (value instanceof Boolean)
{
return value.toString();
}else if (value instanceof JSONArray)
{
JSONArray arr = (JSONArray)value;
if(!arr.isEmpty()){return arr.toJSONString();}
return "";
}else if (value instanceof LinkedHashMap)
{
return value.toString();
}else if (value instanceof float)
{
return value.toString();
}else{
return value.toString();
}
}
catch(Exception e){
return "null"; //此处设为"null“字符串,与实际返回null保持一致
}
}
}
Test2.java