在从Json提取Jmeter的值时需要帮助

qaxu7uf2  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(198)

我的Json如下所示,我想提取所有“code”值的json,并将它们用逗号分隔。**我有近250个代码值,并希望它们如下所示
RFI 027、RFI 037、RFI 407、RFI 055、RFI 457、RFI 677、RFI 068和RFI 086

{
   "totalDocs":202,
   "recordBatchSize":224,
   "listingType":31,
   "currentPageNo":1,
   "recordStartFrom":18,
   "columnHeader":[
      {
         "id":"0",
         "fieldName":"commId",
         "isCustomAttributeColumn":false,
         "isActive":false
      },
      {
         "id":"24264704",
         "function":"",
         "funParams":"",
         "wrapData":"",
      },
       {
         "code":"RFI027",
         "noOfActions":0,
         "observationId":0         
      },
      {
         "code":"RFI037",
         "noOfActions":0,
         "observationId":0         
      },
      {
         "code":"RFI407",
         "noOfActions":0,
         "observationId":0         
      },      
      {
         "code":"RFI055",
         "noOfActions":0,
         "observationId":0         
      },
      {
         "code":"RFI457",
         "noOfActions":0,
         "observationId":0         
      },
      {
         "code":"RFI677",
         "noOfActions":0,
         "observationId":0         
      },
      {
         "code":"RFI068",
         "noOfActions":0,
         "observationId":0         
      },      
      {
         "code":"RFI086",
         "noOfActions":0,
         "observationId":0         
      },
   ],
   "sortField":"updated",
   "sortFieldType":"timestamp",
   "sortOrder":"desc",
   "editable":true,
   "isIncludeSubFolder":true,
   "totalListData":0
}

我尝试在Jmeter Json提取器中使用$..代码,但它只返回一个值。但我希望输出类似RFI 027,RFI 037,RFI 407,RFI 055,RFI 457,RFI 677,RFI 068,RFI 086。因为我希望在另一个请求中传递所有值。**我尝试使用0,1,2,3和-1匹配号,但它只返回一个值,而对于-1,它返回${ref_formCode1}。**感谢您的帮助。在此先表示感谢。

编辑:

实现JSR223 post-processer后显示空白字段,截图如下。

amrnrhlw

amrnrhlw1#

您可以使用JSR223 post-processer和下面的代码来实现这一点,同时注意JSON中有一些语法错误,
JSR223 post-processer添加到您的请求中,这将满足您的要求

import groovy.json.JsonSlurper;

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

response.columnHeader.code.each {
  Code->if (Code == null) {}
  else {
    CodeFile += Code + ','  //this will have your code but there will be ',' at the last
  }
}

def CodeFileList = CodeFile.subSequence(0, CodeFile.length() - 1) // this will remove the last ,
log.info('CodeFile:' + CodeFileList)
vars.put("CodeList",CodeFileList)

如果代码值在数据内

import groovy.json.JsonSlurper;

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

response.data.code.each {Code->
  if (Code == null) {}
  else {
    CodeFile += Code + ','  //this will have your code but there will be ',' at the last
  }
}

def CodeFileList = CodeFile.subSequence(0, CodeFile.length() - 1) // this will remove the last ,
log.info('CodeList:' + CodeFileList)
vars.put("CodeList",CodeFileList)

用法:在请求正文内

在请求URL内


指令集
在另一控制器中,


指令集

编辑在主后问:


指令集

68bkxrlz

68bkxrlz2#

您即将完成,您只需要:
1.将“匹配编号”设置为-1
1.勾选Compute concatenation var

它将为您提供${ref_formCode1_ALL}JMeter Variable,其中包含与您的JsonPath查询匹配的所有代码(以逗号分隔):

更多信息:How to Use the JSON Extractor For Testing

相关问题