如何在JMeter中用JSR223后置处理器复用Modalities构造请求JSON
在JMeter中复用响应的Modalities构造请求JSON
问题说明
在JMeter中发送请求后,收到的JSON响应里包含数量可变的modalities数组,需要在后续请求的JSON体中完整复用这些modalities数据。已编写部分Groovy代码,但不知道如何构造符合要求的请求JSON结构。
响应示例
{"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}
目标请求示例
{"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}
现有代码
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); }
解决方案
你现有的代码仅遍历打印了modalities的字段,没有组装成完整的请求JSON结构。可以通过Groovy的JsonBuilder直接构造符合要求的JSON字符串,并存储到JMeter变量中,供后续请求使用:
import groovy.json.JsonSlurper import groovy.json.JsonBuilder // 解析响应的JSON数据 def responseJson = prev.getResponseDataAsString() def slurper = new JsonSlurper() def parsedData = slurper.parseText(responseJson) // 构造目标请求的JSON结构,直接复用响应中的所需字段 def requestBody = [ studyDTO: parsedData.studyDTO, allVisits: true, modalities: parsedData.modalities, includePS: null ] // 将构造好的JSON转为字符串,存入JMeter变量 def builder = new JsonBuilder(requestBody) vars.put("targetRequestJson", builder.toString())
使用方法
在后续请求的Body Data中,直接引用变量${targetRequestJson}即可,JMeter会自动替换为构造好的JSON内容。
内容的提问来源于stack exchange,提问作者MAX GRIMM
相关产品推荐
相关产品推荐

