jmeter Json基于条件过滤数据

jmeter Json基于条件过滤数据,json,parsing,jmeter,Json,Parsing,Jmeter,我正在使用jmeter并试图从Json响应中获取值。 我有以下建议: `{ "linked": {}, "learning_items": [ { "id": "3452", "enrollable_id": "3452", "enrollable_type": "Enrollment", "learnable_id": "6", "learnable_type": "CourseTemplate", "ti

我正在使用jmeter并试图从Json响应中获取值。 我有以下建议:

`{
  "linked": {},
  "learning_items": [
    {
      "id": "3452",
      "enrollable_id": "3452",
      "enrollable_type": "Enrollment",
      "learnable_id": "6",
      "learnable_type": "CourseTemplate",
      "title": "all kinda ",
      "description": "",
      "state": "created",
      "completed_at": null,
      "created_at": "2017-04-10T16:26:36.850-06:00",
      "archived_at": null,
      "permanently_failed": false,
      "data": {
        "end_at": "2017-04-16T23:59:59.000-06:00",
        "expires_at": null,
        "renew_by": null,
        "required": true,
        "score": 0,
        "time_remaining": 5,
        "current_position": 0,
        "furthest_progress": 0,
        "quiz_positions": [
          2,
          3,
          4,
          5
        ],
        "direct_enrollment": true,
        "max_quiz_attempts": null,
        "attempts_count": 0,
        "inactive": null,
        "program": null,
        "programs": [],
        "in_completed_program": false,
        "external_id": null,
        "estimated_time": 5,
        "passing_threshold": 80,
        "has_certificate": false,
        "course_type": "bridge",
        "slide_count": 5,
        "attachments_count": 1,
        "attachments_count_author": 2,
        "third_party_course_id": null,
        "open_book": true,
        "continuing_education_credits": null,
        "features": [
          "has_quizzes"
        ]
      },
      "tags": []
    },
    {
      "id": "3451",
      "enrollable_id": "3451",
      "enrollable_type": "Enrollment",
      "learnable_id": "7",
      "learnable_type": "CourseTemplate",
      "title": "All types",
      "description": "",
      "state": "active",
      "completed_at": null,
      "created_at": "2017-04-10T16:26:36.605-06:00",
      "archived_at": null,
      "permanently_failed": false,
      "data": {
        "end_at": "2017-04-17T23:59:59.000-06:00",
        "expires_at": null,
        "renew_by": null,
        "required": true,
        "score": 0,
        "time_remaining": 7,
        "current_position": 1,
        "furthest_progress": 0.1429,
        "quiz_positions": [
          2,
          3,
          4,
          5,
          6,
          7
        ],
        "direct_enrollment": true,
        "max_quiz_attempts": null,
        "attempts_count": 1,
        "inactive": null,
        "program": null,
        "programs": [],
        "in_completed_program": false,
        "external_id": null,
        "estimated_time": 7,
        "passing_threshold": 80,
        "has_certificate": false,
        "course_type": "bridge",
        "slide_count": 7,
        "attachments_count": 0,
        "attachments_count_author": 1,
        "third_party_course_id": null,
        "open_book": false,
        "continuing_education_credits": null,
        "features": [
          "has_quizzes"
        ]
      },
      "tags": []
    },
    {
      "id": "6301",
      "enrollable_id": "1",
      "enrollable_type": "ProgramEnrollment",
      "learnable_id": "1",
      "learnable_type": "Program",
      "title": "IamaProgram",
      "description": null,
      "state": "active",
      "completed_at": null,
      "created_at": "2018-08-27T13:01:07.383-06:00",
      "archived_at": null,
      "permanently_failed": false,
      "data": {
        "required": true,
        "current_course": {
          "id": 19,
          "title": "abcde",
          "state": "created",
          "end_at": "2018-09-03T23:59:59.999-06:00",
          "expires_at": null,
          "renew_by": null,
          "required": true,
          "score": 0,
          "estimated_time": 2,
          "time_remaining": 2,
          "passing_threshold": 80,
          "has_certificate": null,
          "course_type": "bridge",
          "slide_count": 2,
          "attachments_count": 0,
          "current_position": 0,
          "furthest_progress": 0,
          "learnable_type": "CourseTemplate"
        },
        "end_at": null,
        "expires_at": null,
        "furthest_progress": 0,
        "inactive": null,
        "item_count": 2,
        "item_counts": [
          {
            "item_type": "CourseTemplate",
            "count": 2
          }
        ],
        "program_index": 1,
        "has_certificate": false,
        "pending_approval_item_count": 0,
        "pending_approval_items": [],
        "features": [
          "has_quizzes"
        ]
      },
      "tags": []
    },
    {
      "id": "6300",
      "enrollable_id": "1",
      "enrollable_type": "TaskEnrollment",
      "learnable_id": "1",
      "learnable_type": "Task",
      "title": "IamaCheckpoint",
      "description": "",
      "state": "created",
      "completed_at": null,
      "created_at": "2018-08-27T12:59:42.541-06:00",
      "archived_at": null,
      "permanently_failed": false,
      "data": {
        "requires_approval": false,
        "requires_evidence": false,
        "direct_enrollment": true,
        "required": true,
        "program": null,
        "programs": [],
        "in_completed_program": false,
        "end_at": "2018-09-03T23:59:59.999-06:00",
        "inactive": null,
        "attachments_count": 0,
        "attachments_count_author": 0,
        "has_certificate": false
      },
      "tags": []
    },
    {
      "id": "3450",
      "enrollable_id": "3450",
      "enrollable_type": "Enrollment",
      "learnable_id": "4",
      "learnable_type": "CourseTemplate",
      "title": "Science 101",
      "description": null,
      "state": "complete",
      "completed_at": "2018-08-27T12:37:13.365-06:00",
      "created_at": "2017-04-10T16:26:36.368-06:00",
      "archived_at": null,
      "permanently_failed": false,
      "data": {
        "end_at": "2017-04-17T23:59:59.000-06:00",
        "expires_at": null,
        "renew_by": null,
        "required": true,
        "score": 100,
        "time_remaining": 0,
        "current_position": 0,
        "furthest_progress": 1,
        "quiz_positions": [],
        "direct_enrollment": true,
        "max_quiz_attempts": null,
        "attempts_count": 1,
        "inactive": null,
        "program": null,
        "programs": [],
        "in_completed_program": false,
        "external_id": null,
        "estimated_time": 4,
        "passing_threshold": 80,
        "has_certificate": false,
        "course_type": "bridge",
        "slide_count": 4,
        "attachments_count": 0,
        "attachments_count_author": 0,
        "third_party_course_id": null,
        "open_book": null,
        "continuing_education_credits": null,
        "features": []
      },
      "tags": []
    }
  ],
  "meta": {}
}`
我只想得到所有的课程(
learnable\u id

  • 状态=已创建
  • 可学习的类型=课程模板
一旦我得到满足上述条件的所有
可学习\u id
,我就可以从该数组返回随机数

我怎么得到这个

提前感谢。

导入net.minidev.json.JSONArray;
    import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import org.apache.commons.lang.StringUtils;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;


//Get Store total count
int totalLearningItems = StringUtils.countMatches(new String(data), "enrollable_id");
log.info("Total Number of Learning Items are: " + totalLearningItems);
Random rand = new Random();

if (totalLearningItems > 0) {
    //Check for Fulfilment type is "Pickup"
    String jsonString = new String(data);
    JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
    JSONObject learning_items = (JSONObject) parser.parse(data);
    JSONArray learningArray = (JSONArray) learning_items.get("learning_items");
    String learningItemId;
    courseList = new ArrayList();
    programList = new ArrayList();
    taskList = new ArrayList();

    for (int i=0; i<learningArray.size(); i++) {

            if(learningArray.get(i).getAsString("state").equals("created") || learningArray.get(i).getAsString("state").equals("active")) {
                if(learningArray.get(i).getAsString("learnable_type").equals("CourseTemplate")){
                courseList.add(learningArray.get(i).getAsString("learnable_id"));
                } else if (learningArray.get(i).getAsString("learnable_type").equals("Program")) {
                    programList.add(learningArray.get(i).getAsString("learnable_id"));
                } else if (learningArray.get(i).getAsString("learnable_type").equals("Task")) {
                    taskList.add(learningArray.get(i).getAsString("learnable_id"));
                }               
            }             
    }

    if(courseList.size() > 0) {
        vars.put("courseID", courseList.get(new Random().nextInt(courseList.size())));
    } else if (programList.size() > 0) {
        vars.put("programID", programList.get(new Random().nextInt(programList.size())));
    } else if(taskList.size() > 0) {
        vars.put("taskID", taskList.get(new Random().nextInt(taskList.size())));            
    }     
} 
导入net.minidev.json.JSONObject; 导入net.minidev.json.parser.JSONParser; 导入org.apache.commons.lang.StringUtils; 导入java.util.List; 导入java.util.ArrayList; 导入java.util.Random; //获取存储总计数 int totalEarningItems=StringUtils.countMatches(新字符串(数据),“可注册的_id”); log.info(“学习项目总数为:“+总收入项目”); Random rand=新的Random(); 如果(总收入项目>0){ //检查履行类型是否为“提货” String jsonString=新字符串(数据); JSONParser=newJSONParser(JSONParser.MODE\uJSON\uSimple); JSONObject learning_items=(JSONObject)parser.parse(data); JSONArray learningArray=(JSONArray)learning_items.get(“learning_items”); 字符串学习项ID; courseList=新的ArrayList(); programList=新的ArrayList(); taskList=newarraylist(); 对于(int i=0;i 0){ vars.put(“courseID”,courseList.get(new Random().nextInt(courseList.size())); }else if(programList.size()>0){ vars.put(“programID”,programList.get(new Random().nextInt(programList.size())); }else if(taskList.size()>0){ vars.put(“taskID”,taskList.get(new Random().nextInt(taskList.size())); } }
您可以使用JSON路径通过应用所需的过滤器来提取JSON键值

在您的请求中添加一个作为子级的,您将获得更高级别的响应

在JSON提取器中使用以下配置

 Json path Expression :  $..[?(@.learnable_type=='CourseTemplate' && @.state == 'created')].learnable_id
匹配号:0
(这将从JMeter中可能的匹配返回一个随机matach

如下图所示

调试采样器结果:

更多信息: