在JMETER中使用上一个响应值形成JSON请求

vmjh9lq9  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(158)

应我的请求,我得到以下答复,

{
    "studyDTO": {
        "studyId": 191,
        "studyCode": "test_ispptest2"
    },
    "sites": [],
    "subjects": [],
    "visits": [],
    "sftpLocations": [],
    "dicomLocations": [],
    "fileSystemLocations": [],
    "rawFileSystemLocations": [],
    "states": null,
    "modalities": [
        {
            "studyId": 191,
            "submitValue": "ct",
            "displayValue": "Conventional CT",
            "orderOfDisplay": 30
        },
        {
            "studyId": 191,
            "submitValue": "multi_slice_spiral_ct",
            "displayValue": "Multi-Slice Spiral CT",
            "orderOfDisplay": 50
        },
        {
            "studyId": 191,
            "submitValue": "dxa",
            "displayValue": "DXA",
            "orderOfDisplay": 60
        },
        {
            "studyId": 191,
            "submitValue": "mri",
            "displayValue": "MRI",
            "orderOfDisplay": 100
        },
        {
            "studyId": 191,
            "submitValue": "unknown",
            "displayValue": "Unknown",
            "orderOfDisplay": 240
        }
    ],
    "examDates": [],
    "series": null,
    "transferType": null,
    "customFolder": false,
    "customFile": false,
    "folderStructure": null,
    "fileStructure": null,
    "allSites": true,
    "allSubjects": true,
    "allVisits": true,
    "allStates": false,
    "allExamDates": true,
    "allModalities": false,
    "allSeries": false,
    "softEditOverride": false,
    "includePS": false,
    "includeSR": false,
    "includeRTStruct": false,
    "dicomTemplate": null,
    "errorMessage": null,
    "successMessage": null
}

在响应中,我收到的5 modality值可以更多,在JSON类型的下一个请求主体中,我希望添加所有modalities,我如何使用JSR223后处理器来完成此操作,
申请样品:

{
    "studyDTO": {
        "studyId": 191,
        "studyCode": "test_ispptest2"
    },
    "allVisits": true,
    "modalities": [
        {
            "studyId": 191,
            "submitValue": "ct",
            "displayValue": "Conventional CT",
            "orderOfDisplay": 30
        },
        {
            "studyId": 191,
            "submitValue": "multi_slice_spiral_ct",
            "displayValue": "Multi-Slice Spiral CT",
            "orderOfDisplay": 50
        },
        {
            "studyId": 191,
            "submitValue": "dxa",
            "displayValue": "DXA",
            "orderOfDisplay": 60
        },
        {
            "studyId": 191,
            "submitValue": "mri",
            "displayValue": "MRI",
            "orderOfDisplay": 100
        },
        {
            "studyId": 191,
            "submitValue": "unknown",
            "displayValue": "Unknown",
            "orderOfDisplay": 240
        }
    ],
    "includePS": null
}

到目前为止我已经开发了,但是不知道如何形成请求JSON

import groovy.json.JsonSlurper
def jsonString = prev.getResponseDataAsString();
def jsonConvert = new JsonSlurper();
def object = jsonConvert.parseText(jsonString);
def modalityS = object.modalities.size().toString();
def modalitySize = modalityS?.isInteger() ? modalityS.toInteger() : null
for (int i = 0; i < modalitySize ; i++) {
    def modalityOrderOfDisplay = object.modalities[i].orderOfDisplay;
    def modalSubmitValue = object.modalities[i].submitValue;
    def modalDisplayValue = object.modalities[i].displayValue;
    log.info('----------------------->'+modalityOrderOfDisplay);
    log.info('----------------------->'+modalSubmitValue);
    log.info('----------------------->'+modalDisplayValue);
}
niknxzdl

niknxzdl1#

看一看JsonBuilder
类似于:

def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())

def request = [:]

request.put('studyDTO', response.studyDTO)
request.put('allVisits', response.allVisits)
request.put('modalities', response.modalities)
request.put('includePS', null)

vars.put('request', new groovy.json.JsonBuilder(request).toPrettyString())

应该能帮上忙。
如果需要,您可以将生成的请求正文引用为${request}
更多信息:

相关问题