简体   繁体   English

需要使用 Jmeter 中的正则表达式在单个表达式中提取 2 个值

[英]Need to extract 2 values in a single expression using regular expression in Jmeter

Here's my input:这是我的输入:

[{affectedRows:0,data:[{dotprojectseqnum:1,brandName:"Curries",companyId:166,newOrExisting:"Imported",projectCode:"230822_5",projectDesc:"AMBER POCASSET HIGH SCHOOL",framePoNumber:"8700",lastchange:new Date(1661252342997),masterProject:"Not Assigned to Any",projectId:149235,rowID:1},{dotprojectseqnum:2,brandName:"Curries",companyId:166,newOrExisting:"Imported",projectCode:"230822_1",projectDesc:"AMBER POCASSET HIGH SCHOOL",framePoNumber:"8123",lastchange:new Date(1661252330273),masterProject:"Not Assigned to Any",projectId:149234,rowID:2},{dotprojectseqnum:0,brandName:"Curries",companyId:166,newOrExisting:"Imported",projectCode:"230822_3",projectDesc:"AMBER POCASSET HIGH SCHOOL",framePoNumber:"1064892",lastchange:new Date(1661252328280),masterProject:"Not Assigned to Any",projectId:149233,rowID:3},{dotprojectseqnum:1,brandName:"Curries",companyId:166,newOrExisting:"Imported",projectCode:"CuLoTestFR5",projectDesc:"AMBER POCASSET HIGH SCHOOL",framePoNumber:"3589",lastchange:new Date(1661204993723),masterProject:"Not Assigned to Any",projectId:149228,rowID:4},{dotprojectseqnum:2,brandName:"Curries",companyId:166,newOrExisting:"Imported",projectCode:"CuLoTestDR2",projectDesc:"AMBER POCASSET HIGH SCHOOL",doorPoNumber:"6539",framePoNumber:"9132",lastchange:new Date(1661204974390),masterProject:"Not Assigned to Any",projectId:149227,rowID:5},{dotprojectseqnum:0,brandName:"Curries",companyId:166,newOrExisting:"Imported",projectCode:"CuLoTestFR3",projectDesc:"AMBER POCASSET HIGH SCHOOL",framePoNumber:"1066892",lastchange:new Date(1661204971227),masterProject:"Not Assigned to Any",projectId:149226,rowID:6},{brandName:"Curries",ppof:"Y",newOrExisting:"New",rowID:7,dotprojectseqnum:0,companyId:166,projectCode:"ppof28",imported:"N",projectDesc:"jkj",lastchange:new Date(1640697895847),masterProject:"Not Assigned to Any",projectId:144412,selected:0},{brandName:"Curries",ppof:"Y",newOrExisting:"New",rowID:8,dotprojectseqnum:0,companyId:166,projectCode:"12272021",imported:"N",projectDesc:"PPOF ",lastchange:new Date(1640659904817),masterProject:"Not Assigned to Any",projectId:144385,selected:0},{brandName:"Curries",ppof:"Y",newOrExisting:"New",rowID:9,dotprojectseqnum:0,companyId:166,projectCode:"2-10-21",imported:"N",projectDesc:"PPOF",lastchange:new Date(1613013416263),masterProject:"Not Assigned to Any",projectId:130500,selected:0}],endRow:9,invalidateCache:false,isDSResponse:true,operationType:"fetch",queueStatus:0,startRow:0,status:0,timing:{name:"Server processing",start:1661245642852,children:[{name:"RPCManager construction",start:1661245642852,children:[{name:"DSRequest construction",start:1661245642912,children:[],end:1661245642917}],end:1661245642917},{name:"RPCManager processing",start:1661245642917,children:[{name:"DSRequest processing",start:1661245642917,children:[{name:"groupBy processing",start:1661245642917,children:[],end:1661245642917},{name:"includeFrom processing",start:1661245642917,children:[],end:1661245642917},{name:"Declarative security checks",start:1661245642917,children:[],end:1661245642917},{name:"Field-level declarative security checks",start:1661245642917,children:[],end:1661245642917},{name:"Prepare DSRequest for DMI",start:1661245642917,children:[],end:1661245642917},{name:"DMI",start:1661245642917,children:[],end:1661245642917},{name:"transformMultipleFields processing",start:1661245642917,children:[],end:1661245642917},{name:"Rowcount query",start:1661245642917,children:[{name:"Unpaged fetch",start:1661245642917,children:[{name:"get connection",start:1661245642917,children:[],end:1661245642917},{name:"query",start:1661245642917,children:[],end:1661245642957},{name:"SQLTransform",start:1661245642957,children:[],end:1661245642957}],end:1661245642957}],end:1661245642957},{name:"Paged fetch",start:1661245642957,children:[{name:"get connection",start:1661245642957,children:[],end:1661245642957},{name:"query",start:1661245642957,children:[],end:1661245643017},{name:"SQLTransform",start:1661245643017,children:[],end:1661245643017}],end:1661245643017}],end:1661245643017}],end:1661245643017},{name:"DSResponse serialization",start:1661245643017,end:1661245643017}],end:1661245643017},totalRows:9}]

From the above, I need to get only the projectcode and the respective projectId excluding all other data from the list.从上面,我只需要获取projectcode和相应的projectId ,不包括列表中的所有其他数据。

In this example:在这个例子中:

projectCode:"230822_1", projectId:149234

Can anyone help me to solve this?谁能帮我解决这个问题?

JSON is not a regular language so I believe it's better to use JSON Extractor for this. JSON 不是常规语言,所以我相信最好使用JSON 提取器 The question is which one do you want?问题是你想要哪一个? First?第一的? Second?第二? Random?随机的? All?全部?

Example setup:示例设置:

在此处输入图像描述

More information: How to Use the JSON Extractor For Testing更多信息:如何使用 JSON 提取器进行测试

You JSON is incorrect, there are few syntax errors.你 JSON 不正确,语法错误很少。 Just fixed it, if you JSON is like below and you want to fetch all the projectCode and associate project ID (if I understood your requirement clearly).刚刚修复它,如果您 JSON 如下所示,并且您想获取所有项目代码并关联项目 ID(如果我清楚地理解您的要求)。

[
    {
        "affectedRows": 0,
        "data": [
            {
                "dotprojectseqnum": 1,
                "brandname": "Curries",
                "companyId": 166,
                "newOrExisting": "Imported",
                "projectCode": "230822_5",
                "projectDesc": "AMBER POCASSET HIGH SCHOOL",
                "framePoNumber": "8700",
                "lastchange": "new Date(1661252342997)",
                "masterProject": "Not Assigned to Any",
                "projectId": 149235,
                "rowID": 1
            },
            {
                "dotprojectseqnum": 2,
                "brandname": "Curries",
                "companyId": 166,
                "newOrExisting": "Imported",
                "projectCode": "230822_1",
                "projectDesc": "AMBER POCASSET HIGH SCHOOL",
                "framePoNumber": "8123",
                "lastchange": "new Date(1661252330273)",
                "masterProject": "Not Assigned to Any",
                "projectId": 149234,
                "rowID": 2
            },
            {
                "dotprojectseqnum": 0,
                "brandname": "Curries",
                "companyId": 166,
                "newOrExisting": "Imported",
                "projectCode": "230822_3",
                "projectDesc": "AMBER POCASSET HIGH SCHOOL",
                "framePoNumber": "1064892",
                "lastchange": "new Date(1661252328280)",
                "masterProject": "Not Assigned to Any",
                "projectId": 149233,
                "rowID": 3
            },
            {
                "dotprojectseqnum": 1,
                "brandname": "Curries",
                "companyId": 166,
                "newOrExisting": "Imported",
                "projectCode": "CuLoTestFR5",
                "projectDesc": "AMBER POCASSET HIGH SCHOOL",
                "framePoNumber": "3589",
                "lastchange": "new Date(1661204993723)",
                "masterProject": "Not Assigned to Any",
                "projectId": 149228,
                "rowID": 4
            },
            {
                "dotprojectseqnum": 2,
                "brandname": "Curries",
                "companyId": 166,
                "newOrExisting": "Imported",
                "projectCode": "CuLoTestDR2",
                "projectDesc": "AMBER POCASSET HIGH SCHOOL",
                "doorPoNumber": "6539",
                "framePoNumber": "9132",
                "lastchange": "new Date(1661204974390)",
                "masterProject": "Not Assigned to Any",
                "projectId": 149227,
                "rowID": 5
            },
            {
                "dotprojectseqnum": 0,
                "brandname": "Curries",
                "companyId": 166,
                "newOrExisting": "Imported",
                "projectCode": "CuLoTestFR3",
                "projectDesc": "AMBER POCASSET HIGH SCHOOL",
                "framePoNumber": "1066892",
                "lastchange": "new Date(1661204971227)",
                "masterProject": "Not Assigned to Any",
                "projectId": 149226,
                "rowID": 6
            },
            {
                "brandname": "Curries",
                "ppof": "Y",
                "newOrExisting": "New",
                "rowID": 7,
                "dotprojectseqnum": 0,
                "companyId": 166,
                "projectCode": "ppof28",
                "imported": "N",
                "projectDesc": "jkj",
                "lastchange": "new Date(1640697895847)",
                "masterProject": "Not Assigned to Any",
                "projectId": 144412,
                "selected": 0
            },
            {
                "brandname": "Curries",
                "ppof": "Y",
                "newOrExisting": "New",
                "rowID": 8,
                "dotprojectseqnum": 0,
                "companyId": 166,
                "projectCode": "12272021",
                "imported": "N",
                "projectDesc": "PPOF ",
                "lastchange": "new Date(1640659904817)",
                "masterProject": "Not Assigned to Any",
                "projectId": 144385,
                "selected": 0
            },
            {
                "brandname": "Curries",
                "ppof": "Y",
                "newOrExisting": "New",
                "rowID": 9,
                "dotprojectseqnum": 0,
                "companyId": 166,
                "projectCode": "2-10-21",
                "imported": "N",
                "projectDesc": "PPOF",
                "lastchange": "new Date(1613013416263)",
                "masterProject": "Not Assigned to Any",
                "projectId": 130500,
                "selected": 0
            }
        ],
        "endRow": 9,
        "invalidateCache": false,
        "isDSResponse": true,
        "operationType": "fetch",
        "queueStatus": 0,
        "startRow": 0,
        "status": 0,
        "timing": {
            "name": "Server processing",
            "start": 1661245642852,
            "children": [
                {
                    "name": "RPCManager construction",
                    "start": 1661245642852,
                    "children": [
                        {
                            "name": "DSRequest construction",
                            "start": 1661245642912,
                            "children": [],
                            "end": 1661245642917
                        }
                    ],
                    "end": 1661245642917
                },
                {
                    "name": "RPCManager processing",
                    "start": 1661245642917,
                    "children": [
                        {
                            "name": "DSRequest processing",
                            "start": 1661245642917,
                            "children": [
                                {
                                    "name": "groupBy processing",
                                    "start": 1661245642917,
                                    "children": [],
                                    "end": 1661245642917
                                },
                                {
                                    "name": "includeFrom processing",
                                    "start": 1661245642917,
                                    "children": [],
                                    "end": 1661245642917
                                },
                                {
                                    "name": "Declarative security checks",
                                    "start": 1661245642917,
                                    "children": [],
                                    "end": 1661245642917
                                },
                                {
                                    "name": "Field-level declarative security checks",
                                    "start": 1661245642917,
                                    "children": [],
                                    "end": 1661245642917
                                },
                                {
                                    "name": "Prepare DSRequest for DMI",
                                    "start": 1661245642917,
                                    "children": [],
                                    "end": 1661245642917
                                },
                                {
                                    "name": "DMI",
                                    "start": 1661245642917,
                                    "children": [],
                                    "end": 1661245642917
                                },
                                {
                                    "name": "transformMultipleFields processing",
                                    "start": 1661245642917,
                                    "children": [],
                                    "end": 1661245642917
                                },
                                {
                                    "name": "Rowcount query",
                                    "start": 1661245642917,
                                    "children": [
                                        {
                                            "name": "Unpaged fetch",
                                            "start": 1661245642917,
                                            "children": [
                                                {
                                                    "name": "get connection",
                                                    "start": 1661245642917,
                                                    "children": [],
                                                    "end": 1661245642917
                                                },
                                                {
                                                    "name": "query",
                                                    "start": 1661245642917,
                                                    "children": [],
                                                    "end": 1661245642957
                                                },
                                                {
                                                    "name": "SQLTransform",
                                                    "start": 1661245642957,
                                                    "children": [],
                                                    "end": 1661245642957
                                                }
                                            ],
                                            "end": 1661245642957
                                        }
                                    ],
                                    "end": 1661245642957
                                },
                                {
                                    "name": "Paged fetch",
                                    "start": 1661245642957,
                                    "children": [
                                        {
                                            "name": "get connection",
                                            "start": 1661245642957,
                                            "children": [],
                                            "end": 1661245642957
                                        },
                                        {
                                            "name": "query",
                                            "start": 1661245642957,
                                            "children": [],
                                            "end": 1661245643017
                                        },
                                        {
                                            "name": "SQLTransform",
                                            "start": 1661245643017,
                                            "children": [],
                                            "end": 1661245643017
                                        }
                                    ],
                                    "end": 1661245643017
                                }
                            ],
                            "end": 1661245643017
                        }
                    ],
                    "end": 1661245643017
                },
                {
                    "name": "DSResponse serialization",
                    "start": 1661245643017,
                    "end": 1661245643017
                }
            ],
            "end": 1661245643017
        },
        "totalRows": 9
    }
]

Please add a JSR223 post-processer with the following code,请使用以下代码添加JSR223 post-processer

import groovy.json.JsonSlurper;

def response = new groovy.json.JsonSlurper().parse(prev.getResponseData());
def totalProjectSize = response[0].data.size().toString();
def projectInit = totalProjectSize?.isInteger() ? totalProjectSize.toInteger() : null

for (int i=0; i < projectInit; i++) {
    def projectCode =  response[0].data[i].projectCode;
    def projectID  =  response[0].data[i].projectId;
    log.info('ProjectCode: '+projectCode+ " & Project Code: "+projectID);
    }

在此处输入图像描述

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

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