Здравствуйте, Как извлечь массив значений и установить его для следующего запроса по одному - PullRequest
0 голосов
/ 28 января 2019

Как извлечь массив значений из ответа и установить запрос следующего сообщения в 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 полей.

Ответы [ 2 ]

0 голосов
/ 30 января 2019

{"chanceKey": "$ {chanceKey}", "activityType": "$ {activityType}", "pdfLanguageCode": "$ {languageCode}", "countryCode": "$ {countryCode}", "Division":" $ {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 ":"$ {regfeature_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} "}]," разное ": []}

0 голосов
/ 29 января 2019

Ваше требование неясно, т. Е. 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. При необходимости сгенерируйте полезную нагрузку JSON как ${selectedFeatures}, то есть на вкладке «Данные тела» следующего сэмплера HTTP Request .

Демо:

enter image description here

Ссылки:

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...