当键未知时如何从jmeter中的json中提取值? [英] How to extract values from json in jmeter when the keys are unkown?
问题描述
我有一个 json 响应,{'sadasd123242':'asdadada122dfsfs','dadsadaskljk':'adasdasdasdsadds'}我想使用 JSON 提取器从 jmeter 测试中的响应中提取密钥.我无法执行此操作,因为我不知道响应中的键.我如何获得钥匙?
假设您有以下格式的响应:
<代码>{数据": {资产":{"sadsad12dwqqwe": "asda1212312","asdasd1213123": "asdas2131231"}}}
您可以使用
参考文献:
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"
}
}
}
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())
}
It will print key names into jmeter.log file and create JMeter Variables like:
- `${key_1}`
- `${key_2}`
- etc.
holding the required "key" values.
Demo:
References:
这篇关于当键未知时如何从jmeter中的json中提取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!