如何使用正则表达式提取器在jmeter中提取json响应数据?

时间:2022-06-01 20:10:34

I am just trying to extract json response data using jmeter but not able to do so.
I am getting something like {"authorizationToken":"abcdef"}.
I am trying to get authorizationToken but not able to get this.

我只是尝试使用jmeter来提取json响应数据,但不能这样做。我得到了类似{“authorizationToken”:“abcdef”}的内容。我正在尝试获得授权令牌,但无法获得。

Can anyone help me getting this working?

谁能帮我把它修好吗?

1 个解决方案

#1


14  

Is authorizationToken appearance unique in response (does it appear only once)?

authorizationToken外观在响应中是否惟一(是否只出现一次)?

If it does you may simply use Regular Expression Extractor added to the HTTP Request which returns json response, with regex like following:

如果是的话,您可以使用添加到返回json响应的HTTP请求中的正则表达式提取器,regex如下:


HTTP Request
    Regular Expression Extractor
    Reference Name: authToken
    Regular Expression: "authorizationToken":"(.+?)"
    Template: $1$
    Match No.: 1

and refer further extracted value as ${authToken}.

并将进一步提取的值引用为${authToken}。


But if your case is more complicated and there several appearances of authorizationToken in json response and you have to extract concrete one you may use e.g. BeanShell PostProcessor / BSF PostProcessor added to the same HTTP Request to extract value with beanshell code + json processing library.

但是,如果您的情况比较复杂,并且在json响应中出现了一些authorizationToken,并且您必须提取具体的authorizationToken,您可以使用BeanShell PostProcessor / BSF PostProcessor添加到相同的HTTP请求中,使用BeanShell代码+ json处理库提取值。

If not - the first solution above should help.

如果没有,上面的第一个解决方案应该会有所帮助。


UPDATE:

更新:

At the moment the most comfortable way to process JSON responses seems to be custom JSON utils for jmeter (JSON Path Assertion, JSON Path Extractor, JSON Formatter) which are also part of Jmeter Plugins.

目前处理JSON响应最舒适的方式似乎是为jmeter (JSON路径断言、JSON路径提取器、JSON格式化程序)定制JSON utils,这也是jmeter插件的一部分。

In this particular case you can use JSON Path Extractor.

在本例中,您可以使用JSON路径提取器。

#1


14  

Is authorizationToken appearance unique in response (does it appear only once)?

authorizationToken外观在响应中是否惟一(是否只出现一次)?

If it does you may simply use Regular Expression Extractor added to the HTTP Request which returns json response, with regex like following:

如果是的话,您可以使用添加到返回json响应的HTTP请求中的正则表达式提取器,regex如下:


HTTP Request
    Regular Expression Extractor
    Reference Name: authToken
    Regular Expression: "authorizationToken":"(.+?)"
    Template: $1$
    Match No.: 1

and refer further extracted value as ${authToken}.

并将进一步提取的值引用为${authToken}。


But if your case is more complicated and there several appearances of authorizationToken in json response and you have to extract concrete one you may use e.g. BeanShell PostProcessor / BSF PostProcessor added to the same HTTP Request to extract value with beanshell code + json processing library.

但是,如果您的情况比较复杂,并且在json响应中出现了一些authorizationToken,并且您必须提取具体的authorizationToken,您可以使用BeanShell PostProcessor / BSF PostProcessor添加到相同的HTTP请求中,使用BeanShell代码+ json处理库提取值。

If not - the first solution above should help.

如果没有,上面的第一个解决方案应该会有所帮助。


UPDATE:

更新:

At the moment the most comfortable way to process JSON responses seems to be custom JSON utils for jmeter (JSON Path Assertion, JSON Path Extractor, JSON Formatter) which are also part of Jmeter Plugins.

目前处理JSON响应最舒适的方式似乎是为jmeter (JSON路径断言、JSON路径提取器、JSON格式化程序)定制JSON utils,这也是jmeter插件的一部分。

In this particular case you can use JSON Path Extractor.

在本例中,您可以使用JSON路径提取器。