如何使用Jmeter Json Extractor获取级别值

时间:2018-03-10 07:10:26

标签: json jmeter jmeter-plugins

我已粘贴下面的json文件,想获得最内层的levelID值(即 ff808181620ce25501620d08e4dc0061 )及其父级( ff808181620ce25501620d08e4b9005e )。相应地为变量名尝试$ .levelID [3]; $。levelID [4]:levelID& parentlevelID但未能获得上面括号中提到的值。

{ "levelID":null, "levelType":0, "levelName":null, "learningObjective":null, "reusablelevelID":"", "reusableClid":"", "duration":null, "subLevels":[{"levelID":"ff808181620ce25501620d08e492005b","levelType":1,"levelName":"ch1","learningObjective":null,"reusablelevelID":"0","reusableClid":"0","duration":null, "subLevels":[{"levelID":"ff808181620ce25501620d08e4b9005e","levelType":2,"levelName":"sch1","learningObjective":null,"reusablelevelID":"0","reusableClid":"0","duration":null, "subLevels":[{"levelID":"ff808181620ce25501620d08e4dc0061","levelType":3,"levelName":"u1","learningObjective":null,"reusablelevelID":"0","reusableClid":"0","duration":null, "subLevels":null,"wbList":[],"cList":null,"classroomResources":[],"qList":[]}],"wbList":[],"cList":null,"classroomResources":[],"qList":[]}],"wbList":[],"cList":null,"classroomResources":[],"qList":[]}], "wbList":[], "cList":[], "classroomResources":[], "qList":[] }

请提前帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

您的levelID位于JSON的第2级([{内),而每个.代表1级。

您需要两个JSON Extractor作为请求的后处理器,两者都具有相同的JSON路径表达式:

$..levelID

对于ff808181620ce25501620d08e4dc0061选择第4场比赛和第3场比赛

它会返回两个不同的变量,包含您要求的两个值。

答案 1 :(得分:1)

  1. 添加JSR223 PostProcessor作为返回JSON以上的请求的子项
  2. 将以下代码放入"脚本"面积:

    import com.jayway.jsonpath.JsonPath
    
    def response = prev.getResponseDataAsString()
    def levelTypes = JsonPath.read(response, '$..levelType').sort()
    vars.put('innerMost', JsonPath.read(response, '$..[?(@.levelType == ' + levelTypes.get(levelTypes.size()-1) + ')].levelID').get(0).toString())
    vars.put('parentOf', JsonPath.read(response, '$..[?(@.levelType == ' + levelTypes.get(levelTypes.size()-2) + ')].levelID').get(0).toString())
    
  3. 完成后,您可以将最内层的ID称为${innterMost},将其父级称为${parentOf}

  4. 参考文献: