JMeter后置处理器之JSON提取器

发布时间:2023年12月22日

?

1、添加json提取器

【线程组】——>【添加】——>【后置处理器】
添加JSON提取器

2、JSON提取器面板介绍

  • Main sampleand sub-samples:应用于主sample及子sample
  • Main sample only:默认的是这个,应用于主sample
  • Sub-samples only:应用于子sample
  • JMeterVariableName to use:应用于变量命名的内容
  • Variable names : 名称
  • JSONPath Expression:JSON表达式
  • Match Numbers:0随机;n取第几个匹配值;-1匹配所有,后续引用用 变量名_N 取第N个值
  • Default Value:未取到值的时候默认值

3、json提取器提取方法介绍:

$.你要提取的参数

1、响应类似{"属性":[{"sx":1},{},{}]},整个响应是个对象。取sx的值:$.属性[0].sx

2、修改json extractor 获取数组的所有值   $[*].属性。match no=3取第3个

3、match no=5,数组越界后,取default values

4、修改json extractor  设置match no =-1 匹配对应字段所有值;并修改接收参数的http请求的参数为 ${varname_N}。N=1-4
我想提取code参数值:
JSON Path Extractor:$.code

提取username参数值:
JSON Path Extractor:$.list[0].username
 

如果提取的参数不存在父级?
$.你要提取的参数名

提取的参数存在父级
$.你要提取的参数名的父亲.你要提取的参数名

如果提取参数的父级存在父级
$.你要提取的参数名的父亲的父亲(爷爷).你要提取的参数名的父亲.你要提取的参数名

参数如果是数组?
$.你要提取的参数参数[N]

以下标的形式取值,0代表取的数组中第一个值,1代表取的是数组中第二个值,N代表取的数组中第N+1个值

4、提取(单个值)

{
    "path": "\/api\/ou\/org\/tree",
    "data": [
        {
            "dLevel": "0",
            "isAble": "1",
            "city": null,
            "creatorId": null,
            "updateTime": 1615276836000,
            "remark": null,
            "sort": 0,
            "type": null,
            "parentId": "1",
            "dLevelCode": "000001",
            "province": null,
            "createTime": 1615276836000,
            "reviserId": null,
            "simpleName": "测试",
            "children": null,
            "tenantId": "",
            "name": "测试数据",
            "contactNumber": null,
            "id": "3f28875b7815fda40178160086c80008",
            "deptCode": "meritdata"
        }
    ],
    "success": true,
    "message": "成功",
    "stackmessage": null,
    "timestamp": 1620357542328,
    "status": 1,
    "extraInfo": {
    }
}
  • 如下所示,是一个提取单个值的示例。

在这里插入图片描述

5、提取(多个值)

??提取多个值得json内容和提取单个值得格式保持一致,多个之间分别用分号来隔开
JSON提取器提取多个值

样例数据

{
    "path": "\/api\/ou\/org\/tree",
    "data": [
        {
            "dLevel": "0",
            "isAble": "1",
            "city": null,
            "creatorId": null,
            "updateTime": 1616567075000,
            "remark": null,
            "sort": 1,
            "type": null,
            "parentId": "3f28875b7815fda40178160086c80008",
            "dLevelCode": "000001000002",
            "province": null,
            "createTime": 1616567075000,
            "reviserId": null,
            "simpleName": "aaaa",
            "children": null,
            "tenantId": "",
            "name": "aaaa",
            "contactNumber": null,
            "id": "3f28875b783e17df017862e80146007b",
            "deptCode": "aaaa"
        },
        {
            "dLevel": "0",
            "isAble": "1",
            "city": null,
            "creatorId": null,
            "updateTime": 1620295113000,
            "remark": "",
            "sort": 1,
            "type": null,
            "parentId": "3f28875b7815fda40178160086c80008",
            "dLevelCode": "000001000003",
            "province": null,
            "createTime": null,
            "reviserId": null,
            "simpleName": "test001",
            "children": null,
            "tenantId": "",
            "name": "test001",
            "contactNumber": null,
            "id": "3f28875b791718ac01792163c22c002e",
            "deptCode": "test001"
        },
        {
            "dLevel": "0",
            "isAble": "1",
            "city": null,
            "creatorId": null,
            "updateTime": 1620355231000,
            "remark": null,
            "sort": 1,
            "type": null,
            "parentId": "3f28875b7815fda40178160086c80008",
            "dLevelCode": "000001000004",
            "province": null,
            "createTime": 1620355231000,
            "reviserId": null,
            "simpleName": "test_001",
            "children": null,
            "tenantId": "",
            "name": "test_001",
            "contactNumber": null,
            "id": "3f28875b793f33c5017944b2b0a104f7",
            "deptCode": "test_001"
        },
        {
            "dLevel": "0",
            "isAble": "1",
            "city": null,
            "creatorId": null,
            "updateTime": 1620355231000,
            "remark": null,
            "sort": 2,
            "type": null,
            "parentId": "3f28875b7815fda40178160086c80008",
            "dLevelCode": "000001000005",
            "province": null,
            "createTime": 1620355231000,
            "reviserId": null,
            "simpleName": "test_002",
            "children": null,
            "tenantId": "",
            "name": "test_002",
            "contactNumber": null,
            "id": "3f28875b793f33c5017944b2b0ef04f9",
            "deptCode": "test_002"
        },
        {
            "dLevel": "0",
            "isAble": "1",
            "city": null,
            "creatorId": null,
            "updateTime": 1620355232000,
            "remark": null,
            "sort": 3,
            "type": null,
            "parentId": "3f28875b7815fda40178160086c80008",
            "dLevelCode": "000001000006",
            "province": null,
            "createTime": 1620355232000,
            "reviserId": null,
            "simpleName": "test_003",
            "children": null,
            "tenantId": "",
            "name": "test_003",
            "contactNumber": null,
            "id": "3f28875b793f33c5017944b2b12e04fb",
            "deptCode": "test_003"
        },
        {
            "dLevel": "0",
            "isAble": "1",
            "city": null,
            "creatorId": null,
            "updateTime": 1620355232000,
            "remark": null,
            "sort": 4,
            "type": null,
            "parentId": "3f28875b7815fda40178160086c80008",
            "dLevelCode": "000001000007",
            "province": null,
            "createTime": 1620355232000,
            "reviserId": null,
            "simpleName": "test_004",
            "children": null,
            "tenantId": "",
            "name": "test_004",
            "contactNumber": null,
            "id": "3f28875b793f33c5017944b2b1ab04fd",
            "deptCode": "test_004"
        },
        {
            "dLevel": "0",
            "isAble": "1",
            "city": null,
            "creatorId": null,
            "updateTime": 1620355232000,
            "remark": null,
            "sort": 5,
            "type": null,
            "parentId": "3f28875b7815fda40178160086c80008",
            "dLevelCode": "000001000008",
            "province": null,
            "createTime": 1620355232000,
            "reviserId": null,
            "simpleName": "test_005",
            "children": null,
            "tenantId": "",
            "name": "test_005",
            "contactNumber": null,
            "id": "3f28875b793f33c5017944b2b20804ff",
            "deptCode": "test_005"
        },
        {
            "dLevel": "0",
            "isAble": "1",
            "city": null,
            "creatorId": null,
            "updateTime": 1620355232000,
            "remark": null,
            "sort": 6,
            "type": null,
            "parentId": "3f28875b7815fda40178160086c80008",
            "dLevelCode": "000001000009",
            "province": null,
            "createTime": 1620355232000,
            "reviserId": null,
            "simpleName": "test_006",
            "children": null,
            "tenantId": "",
            "name": "test_006",
            "contactNumber": null,
            "id": "3f28875b793f33c5017944b2b2660501",
            "deptCode": "test_006"
        }
    ],
    "success": true,
    "message": "成功",
    "stackmessage": null,
    "timestamp": 1620357542356,
    "status": 1,
    "extraInfo": {
    }
}

6、动态传值(多个值)

??使用场景: 场景包含多个接口,多个接口有参数传递;

??样例场景: 测试数据创建创景,包含新建模型、导入属性、生效模型三个主要接口,其中生效模型接口需要拿到新建模型完成后的模型名称和id,作为生效的依据。

??为后续的自动化提供便利。。。。。。

文章来源:https://blog.csdn.net/qq_28203181/article/details/135099167
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。