Доступ к JSON значениям массива с помощью JMeter JSON Extractor - PullRequest
0 голосов
/ 04 августа 2020

У меня есть следующие JSON.

JSON -

{"data": {
    "statusCode": 200,
    "success": true,
    "technicalSettings": [{
            "program": "C:/temp/abc.exe",
            "actions": "9",
            "file_name": "abc1",
            "new_file_name": "newabc1",
            "version": "2.0.0.0",
            "product_name": "abc",
            "description": "abc",
            "eventdate": "20160601120000",
            "autoVoiceProfile": {
                "autoVoices": [{
                        "autoVoiceLanguage": 0,
                        "autoVoiceMessage": [{
                                "name": "AV1",
                                "duration": "1.200000",
                                "checksum": "2d4c44d142bc0391b980b8a103ab35cc23d8f7820895cb6025cf3c829139336c",
                                "fileName": "/usr/g/db/user_autoVoiceMsg7.aifc",
                                "id": 4
                            }, {
                                "name": "AV1",
                                "duration": "0.600000",
                                "checksum": "9538cf287d178964dcb57a05b7acbc00e04c800a9aaed0b22f5433d9dc79d80c",
                                "fileName": "/usr/g/db/user_autoVoiceMsg8.aifc",
                                "id": 4
                            }, {
                                "name": "AV2",
                                "duration": "2.800000",
                                "checksum": "050acdb345e079da1371623c9727bc16d166db0a0b47687ff93d736ddf37cde8",
                                "fileName": "/usr/g/db/user_autoVoiceMsg9.aifc",
                                "id": 5
                            }, {
                                "name": "AV2",
                                "duration": "4.100000",
                                "checksum": "c5a6a39df38505c0c22b75d9ea7781a1755e9c8c9f435e08034f579361ba751c",
                                "fileName": "/usr/g/db/user_autoVoiceMsg10.aifc",
                                "id": 5
                            }
                        ]
                    }
                ],
                "messagesitefilename": null
            }
        }, {
            "program": "C:/temp/abc.exe",
            "actions": "9",
            "file_name": "abc2",
            "new_file_name": "newabc2",
            "version": "2.0.0.0",
            "product_name": "abc",
            "description": "abc",
            "eventdate": "20160601120000",
            "autoVoiceProfile": {
                "autoVoices": [{
                        "autoVoiceLanguage": 0,
                        "autoVoiceMessage": [{
                                "name": "AV1",
                                "duration": "1.200000",
                                "checksum": "2d4c44d142bc0391b980b8a103ab35cc23d8f7820895cb6025cf3c829139336c",
                                "fileName": "/usr/g/db/user_autoVoiceMsg7.aifc",
                                "id": 4
                            }, {
                                "name": "AV1",
                                "duration": "0.600000",
                                "checksum": "9538cf287d178964dcb57a05b7acbc00e04c800a9aaed0b22f5433d9dc79d80c",
                                "fileName": "/usr/g/db/user_autoVoiceMsg8.aifc",
                                "id": 4
                            }, {
                                "name": "AV2",
                                "duration": "2.800000",
                                "checksum": "050acdb345e079da1371623c9727bc16d166db0a0b47687ff93d736ddf37cde8",
                                "fileName": "/usr/g/db/user_autoVoiceMsg9.aifc",
                                "id": 5
                            }
                        ]
                    }
                ],
                "messagesitefilename": null
            }
        }
    ],
    "library": {
        "version": 6,
        "dmIdVersion": 5
    }
},
"success": true,
"statusCode": 200,
"errorMessage": ""

}

Я использую JSON Extractor для получения значений TechnicalSettings. Значения присваиваются переменной pPublishTechSettings.

JSON Extractor

Now I want to access each data in the variable ${pPublishTechSettings_ALL}. There are two values from this JSON.

I used the variable like ${pPublishTechSettings_0}, ${pPublishTechSettings_1} to access the data. But is is working only for ${pPublishTechSettings_1} and which is giving me both the technicalSettings data.

How can I access individual technicalSettings data like ${pPublishTechSettings_0},${pPublishTechSettings_1}...in the BeanShell Sampler?

Note:-

When I use this JSON in the online tool http://www.jsonquerytool.com/ и запрашивают ее как $ .. data..technicalSettings [0], $ .. data..technicalSettings 1 Я получаю правильные значения.

Ответы [ 2 ]

1 голос
/ 04 августа 2020

Используйте Контроллер ForEach .

Используйте выражение пути как: .data.technicalSettings[*]

Ваши JSON настройки экстрактора пути будут такими:

enter image description here

For each controller settings will be like:

enter image description here

Add the request under for each controller and use the ${myvar} in the request. You will get something like this:

введите описание изображения здесь

0 голосов
/ 04 августа 2020

Имейте в виду, что начиная с JMeter 3.1 вы должны использовать JSR223 Test Elements и Groovy язык для написания сценариев, поэтому я дам вам вариант Groovy, который будет выглядеть примерно так:

def technicalSettings = new groovy.json.JsonSlurper().parseText(vars.get('pPublishTechSettings_ALL'))

technicalSettings.eachWithIndex { setting, index ->
    log.info('Setting ' + index + ': ' + new groovy.json.JsonBuilder(setting).toString())
}

Демо:

enter image description here

More information:

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