import java.util.LinkedHashMap; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.Predicate; import net.minidev.json.JSONArray; public class Test { public static void main(String[] args) { String jsonString = "{a:"nnnnn",b:{c:4441,d:dadwadw},e:[f:11,g:22],h:[],i:""}"; String JsonPath,result,name,tmp; int n,m,l,p; //jsonString = prev.getResponseDataAsString(); //log.info("======:"+jsonString); m=4; JsonPath= "$.data["+m+"].gItems.length()"; result =Test.readjson(jsonString,JsonPath); log.info("======================:"+result); l = Integer.parseInt(result); n = 0; for (int i=0;i<le;i++){ JsonPath= "$.data["+n1+"].gItems["+i+"]"; n=i+1; name = "mId_1_"+n; vars.put(name,mId); tmp = vars.get(name); log.info("======================:"+tmp); } System.out.println(resultString); } 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"; } } }
以上就是jsonpath读取使用方法。具体实例如下一行jmeter自动化接口代码
source("D:\资料\Apiauto\jmeter-3.0\lib\java\Test.java"); // Test.java就是上面jsonpath读取数据使用方法 public static void exectFun(){ String rep,jsonPath,jsonPath1,result,result1,tmp; //获取频道页第一屏返回包 rep = prev.getResponseDataAsString(); // log.info("频道页第一屏返回包:"+rep); //获取contents JsonPath= "$.data.hotDestinations.contents.length()"; result =Test.readjson(rep,JsonPath); // log.info("获取contents:"+result); vars.put("contents_length",result); tmp=vars.get("contents_length"); // log.info("获取变量:"+tmp); int lo = Integer.parseInt(result); //-1的目的去掉搜索更多 int l = lo-1; // log.info("检查l为多少:"+l); for(int i=0;i<l;i++){ // 获取每个imageUrl jsonPath = "$.data.hotDestinations.contents["+i+"].title"; // log.info("jsonPath:"+jsonPath); result =Test.readjson(rep,jsonPath); vars.put("title",result); // log.info("获取title:"+result); jsonPath1 = "$.data.hotDestinations.contents["+i+"].appUrl"; // log.info("jsonPath:"+jsonPath); result1 =Test.readjson(rep,jsonPath1); vars.put("appUrl",result1); } }
因此:大家可以根据jsonpath提供读取key的方法,来获取各个节点下的key对应的value值!!!
尤其对与list来讲,jsonpath可以算出长度,来进行循环读取,对于不固定长度的list提供了真正意义上的脚本自动化,难道不是吗~
jsonpath支持多种读取方式,方便快捷~利用jmeter的自身携带的断言语句
Failure = true;
FailureMessage = "title或appurl数据为空";
或
Failure = false
FailureMessage = "本次请求成功";
等完成
举例:
if(checkFlag(flag,tip)==true){
Failure= false;
FailureMessage = "在有优惠券的情况下flag满足于tip对应关系正确,flag值为"+flag+",tip值为"+tip;
}else{
Failure= true;
FailureMessage = "在有优惠券的情况下flag满足于tip对应关系不正确,flag值为"+flag+",tip值为"+tip;
}
建议,在判断某个key为null或者""空时建议加一个try catch
对于jmeter如何输出自动化接口脚本,可以加我或者评论 详细给予解答
!!!!!!!!!!!!!!!!!!!!!1
貌似话已说多~没办法~一旦涉及到自动化,我浑身充满了洪荒之力~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·