Jmeter:使用 special/spaces 个字符提取 JSON 响应

Jmeter: Extracting JSON response with special/spaces characters

你好,谁能帮我提取用户参数的值“testuser1”

我尝试使用此 JSON 路径表达式 $..data 我能够提取整个响应但无法提取用户参数。提前致谢

{ “data”:“{ “took”:13,“timed_out”:false,“_shards”:{ “total”:5,“successful”:5,“skipped”:0,“failed”:0 }, "hits": { "total": 1, "max_score": 1.0, "hits": [ { "_index": "bushidodb_history_network_eval_ea9656ef-0a9b-474b-8026-2f83e2eb9df1_2021-april-10", "_type": "network", " _id”:“6e2e58be-0ccf-3fb4-8239-1d4f2af322e21618059082000”,“_score”:1.0,“_source”:{“misMatches”:[“协议”,“状态”,“命令”],“实例”:“e3032804 -4b6d-3735-ac22-c827950395b4|0.0.0.0|10.179.155.155|53|UDP", "协议": "UDP", "localAddress": "0.0.0.0", "localPort": "12345", "foreignAddress" “:“10.179.155.155”,“foreignPort”:“53”,“命令”:“ping yahoo.com”,“用户”:“testuser1”,“pid”:“10060”,“状态”:“ OUTGOINGFQ", "rate": 216.0, "originalLocalAddress": "192.168.100.229", "exe": "/bin/ping", "md5": "f9ad63ce859 2af407a7be43b7d5de075", "dir" : "", "agentId" : "abcd-dcd123", "year" : "2021", "month" : "APRIL", "day" : "10", "hour" : "12 ", "time": "1618059082000", "isMerged": false, "timestamp": "2021 年 4 月 10 日 12:51:22 PM", "metricKey": "6e2e58be-0ccf-3fb4-8239-1d4f2af322e2", " isCompliant”:假},“排序”:[1618059082000]}]},“聚合”:{“count_over_time”:{“桶”:[{“key_as_string”:“2021-04- 10T08:00:00.000-0400", "key" : 1618056000000, "doc_count" : 1 } ] } }}", “成功”:真实, “信息”: { “代码”:“S”, "message": "获取评估结果算成功" } }

实际响应:

Images

添加对应请求的正则表达式提取器并提取。使用下面的表达式。 表达式:“用户”:“(.*?)”

参考:https://jmeter.apache.org/usermanual/regular_expressions.html

Regular Expression Extractor Sample

你发布的内容对我来说似乎不是有效的 JSON。

如果实际上您得到的是图像中的内容,即:

{
  "data": "{ \"took\" : 13, \"timed_out\" : false, \"_shards\" : { \"total\" : 5, \"successful\" : 5, \"skipped\" : 0, \"failed\" : 0 }, \"hits\" : { \"total\" : 1, \"max_score\" : 1.0, \"hits\" : [ { \"_index\" : \"bushidodb_history_network_eval_ea9656ef-0a9b-474b-8026-2f83e2eb9df1_2021-april-10\", \"_type\" : \"network\", \"_id\" : \"6e2e58be-0ccf-3fb4-8239-1d4f2af322e21618059082000\", \"_score\" : 1.0, \"_source\" : { \"misMatches\" : [ \"protocol\", \"state\", \"command\" ], \"instance\" : \"e3032804-4b6d-3735-ac22-c827950395b4|0.0.0.0|10.179.155.155|53|UDP\", \"protocol\" : \"UDP\", \"localAddress\" : \"0.0.0.0\", \"localPort\" : \"12345\", \"foreignAddress\" : \"10.179.155.155\", \"foreignPort\" : \"53\", \"command\" : \"pingyahoo.com\", \"user\" : \"testuser1\", \"pid\" : \"10060\", \"state\" : \"OUTGOINGFQ\", \"rate\" : 216.0, \"originalLocalAddress\" : \"192.168.100.229\", \"exe\" : \"/bin/ping\", \"md5\" : \"f9ad63ce8592af407a7be43b7d5de075\", \"dir\" : \"\", \"agentId\" : \"abcd-dcd123\", \"year\" : \"2021\", \"month\" : \"APRIL\", \"day\" : \"10\", \"hour\" : \"12\", \"time\" : \"1618059082000\", \"isMerged\" : false, \"timestamp\" : \"Apr10, 202112: 51: 22PM\", \"metricKey\" : \"6e2e58be-0ccf-3fb4-8239-1d4f2af322e2\", \"isCompliant\" : false }, \"sort\" : [ 1618059082000 ] } ] }, \"aggregations\" : { \"count_over_time\" : { \"buckets\" : [ { \"key_as_string\" : \"2021-04-10T08: 00: 00.000-0400\", \"key\" : 1618056000000, \"doc_count\" : 1 } ] } }}",
  "success": true,
  "message": {
    "code": "S",
    "message": "Get Eval results Count Success"
  }
}

最简单的方法就是使用 2 JSON Extractors:

  1. 从响应中提取 data 属性值到 JMeter 变量中

  2. ${data} JMeter变量中提取user属性值到JMeter变量中:

演示:

如果回复与您发布的完全一样,您将无法使用 JSON 提取器,并且必须将其视为普通文本,因此您的选择仅限于 Regular Expression Extractor,例如常规表达式:

"user"\s*:\s*"(\w+)"