JMeter之Json、正则、边界提取器用法

发布于:2022-11-09 ⋅ 阅读:(1169) ⋅ 点赞:(1)

Json提取器属于JMeter的后置处理器, 所谓后置提取器就是请求结束后, 对响应结果进行变量提取, 提取变量是为了验证变量是否符合预期或者将变量值作为全局变量, 以供其他请求使用.

语法:

JSON的基本语法就是.然后跟要取的字段名,比如要取data,就直接写.data,如果要取msg,就直接写

JSON Extractor使用json path表达式匹配,可以一次取多个变量值。$表示响应的根对象。取子对象或对象的属性用. 取数组里的对象用[],数组索引从0开始。

操作符使用:

JsonPath

描述

$

根节点

@

当前节点

.or[]

子节点

..

选择所有符合条件的节点

*

所有节点

[]

迭代器标示,如数组下标

[,]

支持迭代器中做多选

[start:end:step]

数组切片运算符

?()

支持过滤操作

()

支持表达式计算

Json提取器:

 

Json 提取器参数介绍:

Names of created variables:接收值的变量名,多个变量时用分号分隔

Json path:json path表达式,多个表达式用分号分隔

Match no: 0随机;n取第几个匹配值;-1匹配所有,后续引用 变量名_N 取第N个值

Compute comcatemation var(suffix_ALL):如果发现许多结果,插件将使用" , "分隔符将它们连接起来,并将其存储在名为<variable name>_ALL的var中

Default values: 默认值,匹配不到值的时候取该值

Json提取器语法完整说明详见

https://github.com/json-path/JsonPath

Json example:

{
        "store": {
        "book": [
        {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
        },
        {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
        },
        {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
        },
        {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
        }
        ],
        "bicycle": {
        "color": "red",
        "price": 19.95
        }
        },
        "expensive": 10
        }

使用JsonPathTester来测试书写的Json提取器Expression是否能正常工作

1. 获取某一层中value:

获取title这个key的value

$.store.book[0].title

获取到:Sayings of the Century

2. 获取列表下全部某一个元素的value:

获取price这个key的所有value值

$.store.book[*].price

获取到:

8.95
12.99
8.99
22.99

使用*号意味着获取所有列表元素

此种情况下返回为一个list,那么如果此list被放入参数var中,我们如果想获取var中某一个值譬如12.99该如何使用呢?答案就是在参数名后边加_n,其中n为编号,从1开始;

${var_2}即可获取到列表中第二个元素即12.99

3.提取某个固定条件下的value

获取title这个key的value在固定条件下

$.store.book[?(@.author=='Nigel Rees')].title

获取到:

Sayings of the Century

4.提取固定条件下多个value

 

5.获取前or 后第N个value

.result.records[2].id是排除前两条数据;.result.records[-2].id 是排除后两条数据;

从上示例中可以看出, 参数名称, 表达式, 默认值一次都有多个,用分号隔开

JsonPath

Result

$.store.book[*].author

The authors of all books

$..author

All authors

$.store.*

All things, both books and bicycles

$.store..price

The price of everything

$..book[2]

The third book

$..book[-2]

The second to last book

$..book[0,1]

The first two books

$..book[:2]

All books from index 0 (inclusive) until index 2 (exclusive)

$..book[1:2]

All books from index 1 (inclusive) until index 2 (exclusive)

$..book[-2:]

Last two books

$..book[2:]

Book number two from tail

$..book[?(@.isbn)]

All books with an ISBN number

$.store.book[?(@.price < 10)]

All books in store cheaper than 10

$..book[?(@.price <= $['expensive'])]

All books in store that are not "expensive"

$..book[?(@.author =~ /.*REES/i)]

All books matching regex (ignore case)

$..*

Give me every thing

$..book.length()

The number of books

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到