简体   繁体   English

正则表达式JSON响应加特林压力工具

[英]Regex JSON response Gatling stress tool

Wanting to capture a variable called scanNumber in the http response loking like this: 想要在http响应中捕获一个名为scanNumber的变量,如下所示:

{"resultCode":"SUCCESS","errorCode":null,"errorMessage":null,"profile":{"fullName":"TestFirstName TestMiddleName TestLastName","memberships":[{"name":"UA Gold Partner","number":"123-456-123-123","scanNumber":"123-456-123-123"}]}}

How can I do this with a regular experssion? 如何定期进行这项工作? The tool I am using is Gatling stress tool (with the Scala DSL) 我使用的工具是Gatling压力工具(使用Scala DSL)

I have tried to do it like this: 我试过这样做:

.check(jsonPath("""${scanNumber}""").saveAs("scanNr")))

But I get the error: 但我得到错误:

---- Errors --------------------------------------------------------------------
> Check extractor resolution crashed: No attribute named 'scanNu      5 (100,0%)
mber' is defined

You were close first time. 你是第一次接近。

What you actually want is: 你真正想要的是:

.check(jsonPath("""$..scanNumber""").saveAs("scanNr")))

or possibly: 或者可能:

.check(jsonPath("""$.profile.memberships[0].scanNumber""").saveAs("scanNr")))

Note that this uses jsonPath, not regular expressions. 请注意,这使用jsonPath,而不是正则表达式。 JsonPath should more reliable than regex for this. JsonPath应该比regex更可靠。

Check out the JsonPath spec for more advanced usage. 查看JsonPath规范以获得更高级的用法。

use this regex to match this in anywhere in json: 使用这个正则表达式来匹配json中的任何地方:

/"scanNumber":"[^"]+"/

and if you want to match just happens in structure you said use: 如果你想匹配只是发生在结构你说使用:

/\{[^{[]+\{[^{[]+\[\{[^{[]*("scanNumber":"[^"]+")/

Since json fields may change its order you should make your regex more tolerant for those changes: 由于json字段可能会更改其顺序,因此您应该使正则表达式更容忍这些更改:

val j = """{"resultCode":"SUCCESS","errorCode":null,"errorMessage":null,"profile":{"fullName":"TestFirstName TestMiddleName TestLastName","memberships":[{"name":"UA Gold Partner","number":"123-456-123-123","scanNumber":"123-456-123-123"}]}}"""
val scanNumberRegx = """\{.*"memberships":\[\{.*"scanNumber":"([^"]*)".*""".r
val scanNumberRegx(scanNumber) = j

scanNumber //String = 123-456-123-123

This will work even if the json fields will be in different order (but of course keep the structure) 即使json字段的顺序不同(但当然保留结构),这也可以工作

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

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