Jmeter - 如何使用多个对象获取 json 中的嵌套对象

Jmeter - How to get nested object in json with multiple object

我有这个 json:

{
    "deviceId": "deviceCustom",
    "moduleId": "custom",
    "properties": {
        "desired": {
            "settings": {
                "ef78c18c-2291-4d15-ae87-d89abb9b1fef": {
                    "name": "elements",
                    "version": "1.0.0",
                    "category": "A1"
                },
                "f4b04c94-4643-4b13-b10c-9a00fbf4ea27": {
                    "name": "tags",
                    "version": "2.0.0",
                    "category": "B1"
                }
            }
        }
    }
}

并且我想单独获取“设置”下的所有对象。例如:

settings_1="f4b04c94-4643-4b13-b10c-9a00fbf4ea27":{"name":"tags","version":"2.0.0","category":"B1"}
settings_2="ef78c18c-2291-4d15-ae87-d89abb9b1fef":{"name":"elements","version":"1.0.0","category":"A1"}
settings_matchNr=2

在 Jmeter 中,我用这个 JSON 路径表达式配置了一个 JSON 提取器:$.properties.desired.settings 但我得到了这个结果:

settings_1={"f4b04c94-4643-4b13-b10c-9a00fbf4ea27":{"name":"tags","version":"2.0.0","category":"B1"},"ef78c18c-2291-4d15-ae87-d89abb9b1fef":{"name":"elements","version":"1.0.0","category":"A1"}}
settings_matchNr=1

我也尝试过将 JSR223 Post 处理器与 Slurper 一起使用,但没有有效结果。 你能帮我吗?

提前致谢。

  1. 添加 JSR223 PostProcessor 作为上述 returns JSON

    请求的子项
  2. 将以下代码放入“脚本”区域:

    new groovy.json.JsonSlurper().parse(prev.getResponseData()).properties.desired.settings.entrySet().eachWithIndex { entry, index ->
        def setting = [:]
        setting.put(entry.getKey(), entry.getValue())
        vars.put('setting_' + (index + 1), new groovy.json.JsonBuilder(setting).toPrettyString())
    }
    
  3. 就是这样,您将能够将提取的 JSON Objects 引用为 ${setting_1}${setting_2}

更多信息: