当密钥未知时,如何从jmeter中的json中提取值? [英] How to extract values from json in jmeter when the keys are unkown?

查看:159
本文介绍了当密钥未知时,如何从jmeter中的json中提取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json响应, { 'sadasd123242':'asdadada122dfsfs', 'dadsadaskljk':'adasdasdasdsadds' } 我想使用JSON提取器从jmeter测试中的响应中提取密钥.我无法执行此操作,因为我不知道响应中的键.如何获取密钥?

I have a json response as, { 'sadasd123242' : 'asdadada122dfsfs', 'dadsadaskljk' : 'adasdasdasdsadds' } I want to extract the keys from the response in jmeter test using the JSON extractor. I am unable to do this as I do not know the keys in the response. How do I get the keys ?

推荐答案

假定您使用以下格式的响应:

Assuming you have the response in the following format:

{
  "data": {
    "assets": {
      "sadsad12dwqqwe": "asda1212312",
      "asdasd1213123": "asdas2131231"
    }
  }
}

您可以使用 JSR223 PostProcessor 和以下代码来提取键名:

You can extract key names using JSR223 PostProcessor and the following code:

new groovy.json.JsonSlurper().parse(prev.getResponseData()).data.assets.eachWithIndex{ def node, int idx ->
    log.info('Key ' + idx + '=' + node.getKey())
    vars.put('key_' + idx, node.getKey())
}

它将键名打印到 jmeter.log 文件中,并创建JMeter变量,例如:

It will print key names into jmeter.log file and create JMeter Variables like:

- `${key_1}`
- `${key_2}`
- etc.

保留必需的键"值.

演示:

参考文献:

  • Apache Groovy: Parsing and producing JSON
  • Apache Groovy - Why and How You Should Use It

这篇关于当密钥未知时,如何从jmeter中的json中提取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆