您好,如何提取值数组并将其设置为下一请求一个接一个

时间:2019-01-28 10:48:16

标签: jmeter jmeter-3.2 jmeter-4.0

如何从响应中提取值数组并设置为JMETER中的下一个发布请求

我尝试使用JSON提取器,但是当我设置所有字段的值都相同时。

{"status":"success","mainCategories":[{"categoryName":"Comfort & Convenience","sortNo":"1","recommendedFeatures":[{"featureId":"MVS-2018-MIC-32","featureName":"Bluetooth® Hands-free Phone System","featureSortNumber":"0","videoURL":"","videoName":"","showVideo":"N","image":"","copy":"","intelligentMobility":"N","steps":[],"isCompleted":"N"},{"featureId":"MVS-2018-MIC-39","featureName":"Tilt steering column","featureSortNumber":"0","videoURL":"","videoName":"","showVideo":"N","image":"","copy":"","intelligentMobility":"N","steps":[],"isCompleted":"N"}],"optionalFeatures":[{"featureId":"MVS-2018-MIC-34","featureName":"Air conditioning","featureSortNumber":"0","videoURL":"","videoName":"","showVideo":"N","image":"","copy":"","intelligentMobility":"N","steps":[],"isCompleted":"N"},{"featureId":"MVS-2018-MIC-36","featureName":"Integrated Key with remote keyless entry","featureSortNumber":"0","videoURL":"","videoName":"","showVideo":"N","image":"","copy":"","intelligentMobility":"N","steps":[],"isCompleted":"N"}]},{"categoryName":"Instrumentation & Driver Controls","sortNo":"1","optionalFeatures":[{"featureId":"MVS-2018-MIC-33","featureName":"Trip computer (average fuel economy, current fuel economy, distance to empty)","featureSortNumber":"0","videoURL":"","videoName":"","showVideo":"N","image":"","copy":"","intelligentMobility":"N","steps":[],"isCompleted":"N"},{"featureId":"MVS-2018-MIC-40","featureName":"Cruise control with steering-wheel-mounted controls",

在这里,我必须提取featureID和isCompleted值并设置为下一个请求。对于这两个字段,我得到的填充值相同。

2 个答案:

答案 0 :(得分:1)

您的要求不清楚,即JSON格式不正确,“功能”选择的标准含糊,等等,因此我将基于对您要实现的目标的有限了解来尝试提供答案。

  1. JSR223 PostProcessor添加为返回上述JSON的请求的子代
  2. 将以下代码放入“脚本”区域:

    def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
    def builder = new groovy.json.JsonBuilder()
    def selectedFeatures = new ArrayList()
    json.mainCategories.each {
        category -> category.recommendedFeatures.each {
            recommendedFeature ->
                def feature = new groovy.json.internal.LazyMap()
                feature.put('featureId', recommendedFeature.get('featureId'))
                feature.put('isCompleted',recommendedFeature.get('isCompleted'))
                selectedFeatures.add(feature)
        }
    }
    json.mainCategories.each {
        category -> category.optionalFeatures.each {
            optionalFeature ->
                def feature = new groovy.json.internal.LazyMap()
                feature.put('featureId', optionalFeature.get('featureId'))
                feature.put('isCompleted',optionalFeature.get('isCompleted'))
                selectedFeatures.add(feature)
        }
    }
    builder(selectedFeatures: selectedFeatures.collect())
    vars.put('selectedFeatures', builder.toString())
    log.info(vars.get('selectedFeatures'))
    
  3. 在需要时,即在下一个HTTP Request采样器的“正文数据”标签中,将生成的JSON有效负载引用为${selectedFeatures}

演示:

enter image description here

参考文献:

答案 1 :(得分:0)

{“ opportunityKey”:“ $ {opportunityKey}”,“ activityType”:“ $ {activityType}”,“ pdfLanguageCode”:“ $ {languageCode}”,“ countryCode”:“ $ {countryCode}”,“部门“:” $ {division}“,” salesUserID“:” $ {userId}“,” salesUserFullName“:” $ {salesUserFullName}“,” consultantImageString“:”“,” clientImageString“:”“,” selectedFeatures“:[ {“ featureId”:“ $ {regfeature_g1}”,“ isCompleted”:“ $ {regcompleted_g1}”},{“ featureId”:“ $ {regfeature_g2}”,“ isCompleted”:“ $ {regcompleted_g2}”},{“ featureId“:” $ {regfeature_g3}“,” isCompleted“:” $ {regcompleted_g3}“},{” featureId“:” $ {regfeature_g4}“,” isCompleted“:” $ {regcompleted_g4}“},{” featureId“ :“ $ {regfeature_g5}”,“ isCompleted”:“ $ {regcompleted_g5}”},{“ featureId”:“ $ {regfeature_g6}”,“ isCompleted”:“ $ {regcompleted_g6}”},{“ featureId”:“ $ {regfeature_g7}“,” isCompleted“:” $ {regcompleted_g7}“},{” featureId“:” $ {regfeature_g8}“,” isCompleted“:” $ {regcompleted_g8}“},{” featureId“:” $ { regfeature_g9}“,” isCompleted“:” $ {regcompleted_g9}“}},{” featureId“:” $ {r egfeature_g10}“,” isCompleted“:” $ {regcompleted_g10}“},{” featureId“:” $ {regfeature_g11}“,” isCompleted“:” $ {regcompleted_g11}“},{” featureId“:” $ {regfeature_g12} “,” isCompleted“:” $ {regcompleted_g12}“},{” featureId“:” $ {regfeature_g13}“,” isCompleted“:” $ {regcompleted_g13}“},{” featureId“:” $ {regfeature_g14}“, “ isCompleted”:“ $ {regcompleted_g14}”}],“其他”:[]}