0

我有以下 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 来获取技术设置的值。这些值分配给变量 pPublishTechSettings。

JSON 提取器

现在我想访问变量 ${pPublishTechSettings_ALL} 中的每个数据。此 JSON 中有两个值。

我使用 ${pPublishTechSettings_0}、${pPublishTechSettings_1} 之类的变量来访问数据。但它仅适用于 ${pPublishTechSettings_1} 并且它给了我两个 TechnicalSettings 数据。

如何在 BeanShell Sampler 中访问 ${pPublishTechSettings_0},${pPublishTechSettings_1}... 等个别技术设置数据?

笔记:-

当我在在线工具http://www.jsonquerytool.com/中使用这个 JSON并像 $..data..technicalSettings[0], $..data..technicalSettings 1一样查询它时,我得到了正确的值。

4

2 回答 2

1

使用ForEach 控制器

使用路径表达式为:.data.technicalSettings[*]

您的 JSON 路径提取器设置将如下所示:

在此处输入图像描述

每个控制器的设置如下:

在此处输入图像描述

在每个控制器下添加请求并${myvar}在请求中使用。你会得到这样的东西:

在此处输入图像描述

于 2020-08-04T10:02:42.720 回答
1

请注意,从 JMeter 3.1 开始,您应该使用 JSR223 测试元素和 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())
}

演示:

在此处输入图像描述

更多信息:

于 2020-08-04T16:21:42.540 回答