当密钥未知时,如何从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