gpt4 book ai didi

groovy - 如何使用 Groovy 和 JMeter 提取 json 变量

转载 作者:行者123 更新时间:2023-12-02 09:14:00 25 4
gpt4 key购买 nike

使用 JMeter 3.3 调用 REST API,我得到以下 JSON 响应:

{"map":{},"meta":{"code":"123"}}

如何提取代码 (123) 的值?

到目前为止,我正在使用这个:

  • 线程组
  • HTTP 请求
  • JSON 提取器

使用这 2 个变量:代码;元和这个 json 路径表达式:$.code; $.meta

  • JSR223 断言

使用这段 Groovy 代码:

String codeString =  vars.get("code");

String meta = vars.get("meta");

log.info ("The code answer is " + codeString);

if (codeString != "000"){
AssertionResult.setFailureMessage("The code is: " + codeString + " - meta is: " + meta);

AssertionResult.setFailure(true);
}

这是断言结果:

Assertion error: false
Assertion failure: true
Assertion failure message: The code is: No_Default - meta is: {"code":"000"}

最佳答案

如果您使用 Groovy,则不需要 JSON 路径提取器,您可以像这样验证您的代码:

def code = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..code').get(0).toString()
if (!code.equals('000')) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('The code is ' + code)
}

更多信息:

关于groovy - 如何使用 Groovy 和 JMeter 提取 json 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48884607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com