简体   繁体   English

自定义Jmeter JSON提取器

[英]Customizing Jmeter JSON Extractor

With the JSON Extractor you can save all elements found to an array, then concatenate all array elements if you select "concatenation var. 使用JSON提取器,您可以将找到的所有元素保存到数组中,然后如果选择“ concatenation var。

However how can you customize how the concatenation is done ?. 但是,如何自定义串联的方式呢? Specifically I'd like to wrap each element within double quotes. 具体来说,我想将每个元素都用双引号引起来。 Ex "5723","5796","8901" 例如“ 5723”,“ 5796”,“ 8901”

The default gives you : 5723,5796,8901 默认值是:5723,5796,8901

As per JSON Extractor documentation : 根据JSON Extractor文档

If many results are found, plugin will concatenate them using , separator and store it in a var named <variable name>_ALL 如果找到许多结果,则插件将使用,分隔符将它们连接起来,并将其存储在名为<variable name>_ALL的var中

So as of JMeter 3.3 it is not possible to add any extra characters during concatenation. 因此,从JMeter 3.3开始,无法在串联期间添加任何额外的字符。


If you need to surround the individual results with quotation marks you can use JSR223 PostProcessor and Groovy language instead. 如果需要用引号将各个结果括起来,则可以改用JSR223 PostProcessorGroovy语言。 Groovy has built-in JSON support so you should be able to implement your requirement using code like: Groovy具有内置的JSON支持,因此您应该能够使用以下代码来实现您的要求:

def results = com.jayway.jsonpath.JsonPath.parse(prev.getResponseDataAsString()).read('YOUR_JSONPATH_HERE')
def builder = new StringBuilder()
results.eachWithIndex{ entry, idx ->
    builder.append('"').append(entry).append('"')
    if (idx < results.size() -1) {
        builder.append(',')
    }
}

vars.put('YOUR_VARIABLE_HERE', builder.toString())

More information: Apache Groovy - Why and How You Should Use It 更多信息: Apache Groovy-为什么以及如何使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM