【问题标题】:Regex Extract [Jmeter]正则表达式提取 [Jmeter]
【发布时间】:2015-05-28 21:21:48
【问题描述】:

简而言之:我有一个 json 响应,我需要从中提取一些参数。 (使用jmeter,正则表达式提取器)

json 响应:

{"success":true,"data":[{"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":274253,"iconCls":null,"versionNo":1,"parentId":null,"id":"274253>1>>823132>1","nameId":"823132","name":"***PASTATAS 1 – Upės g. 11, Vilnius, Vilniaus m. sav.","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"TEST_PASTATAS_1","alokAggrType":"OTHER"},{"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":274753,"iconCls":null,"versionNo":1,"parentId":null,"id":"274753>1>>823163>2","nameId":"823163","name":"***PASTATAS 5 – Lvovo 45, Vilnius, Vilniaus m. sav.","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"TEST_PASTATAS_5","alokAggrType":"OTHER"},{"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":275104,"iconCls":null,"versionNo":1,"parentId":null,"id":"275104>1>>823180>3","nameId":"823180","name":"***PASTATAS 11 – Ukmergės 20, Vilnius, Vilniaus m. sav.","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"TEST_PASTATAS_11","alokAggrType":"OTHER"},{"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":275105,"iconCls":null,"versionNo":1,"parentId":null,"id":"275105>1>>823182>4","nameId":"823182","name":"Administracinis pastatas – Naugarduko 3, Vilnius, Vilniaus m. sav.","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"TEST_PASTATAS_12","alokAggrType":"OTHER"},{"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":275106,"iconCls":null,"versionNo":1,"parentId":null,"id":"275106>1>>823184>5","nameId":"823184","name":"***PASTATAS 13 – Grybų 35, Vilnius, Vilniaus m. sav.","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"TEST_PASTATAS_13","alokAggrType":"OTHER"},{"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":275630,"iconCls":null,"versionNo":1,"parentId":null,"id":"275630>1>>823760>6","nameId":"823760","name":"Mano pavadinimas 1 – Pilies 1, Vilniaus m.","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"OBJ_815895","alokAggrType":"OTHER"}],"messages":null}

美化的 json 响应:

{
"success": true,
"data": [{
    "valId": null,
    "oclOclIdIndexType": null,
    "yearno": null,
    "docId": 274253,
    "iconCls": null,
    "versionNo": 1,
    "parentId": null,
    "id": "274253>1>>823132>1",
    "nameId": "823132",
    "name": "***PASTATAS 1 – Upės g. 11, Vilnius, Vilniaus m. sav.",
    "menesiai": null,
    "ivedimas": null,
    "rowTypeIndex": 1,
    "duomensTipas": null,
    "y": null,
    "m1": null,
    "m2": null,
    "m3": null,
    "m4": null,
    "m5": null,
    "m6": null,
    "m7": null,
    "m8": null,
    "m9": null,
    "m10": null,
    "m11": null,
    "m12": null,
    "yo": null,
    "m1o": null,
    "m2o": null,
    "m3o": null,
    "m4o": null,
    "m5o": null,
    "m6o": null,
    "m7o": null,
    "m8o": null,
    "m9o": null,
    "m10o": null,
    "m11o": null,
    "m12o": null,
    "avg": null,
    "editable": null,
    "isParent": true,
    "locked": false,
    "lockMsg": null,
    "icontype": "OBT_364",
    "leaf": null,
    "dptname": null,
    "dpt_dpt_id": null,
    "is_all_dpt_data": null,
    "attId": null,
    "code": "TEST_PASTATAS_1",
    "alokAggrType": "OTHER"
},
{
    "valId": null,
    "oclOclIdIndexType": null,
    "yearno": null,
    "docId": 274753,
    "iconCls": null,
    "versionNo": 1,
    "parentId": null,
    "id": "274753>1>>823163>2",
    "nameId": "823163",
    "name": "***PASTATAS 5 – Lvovo 45, Vilnius, Vilniaus m. sav.",
    "menesiai": null,
    "ivedimas": null,
    "rowTypeIndex": 1,
    "duomensTipas": null,
    "y": null,
    "m1": null,
    "m2": null,
    "m3": null,
    "m4": null,
    "m5": null,
    "m6": null,
    "m7": null,
    "m8": null,
    "m9": null,
    "m10": null,
    "m11": null,
    "m12": null,
    "yo": null,
    "m1o": null,
    "m2o": null,
    "m3o": null,
    "m4o": null,
    "m5o": null,
    "m6o": null,
    "m7o": null,
    "m8o": null,
    "m9o": null,
    "m10o": null,
    "m11o": null,
    "m12o": null,
    "avg": null,
    "editable": null,
    "isParent": true,
    "locked": false,
    "lockMsg": null,
    "icontype": "OBT_364",
    "leaf": null,
    "dptname": null,
    "dpt_dpt_id": null,
    "is_all_dpt_data": null,
    "attId": null,
    "code": "TEST_PASTATAS_5",
    "alokAggrType": "OTHER"
},
{
    "valId": null,
    "oclOclIdIndexType": null,
    "yearno": null,
    "docId": 275104,
    "iconCls": null,
    "versionNo": 1,
    "parentId": null,
    "id": "275104>1>>823180>3",
    "nameId": "823180",
    "name": "***PASTATAS 11 – Ukmergės 20, Vilnius, Vilniaus m. sav.",
    "menesiai": null,
    "ivedimas": null,
    "rowTypeIndex": 1,
    "duomensTipas": null,
    "y": null,
    "m1": null,
    "m2": null,
    "m3": null,
    "m4": null,
    "m5": null,
    "m6": null,
    "m7": null,
    "m8": null,
    "m9": null,
    "m10": null,
    "m11": null,
    "m12": null,
    "yo": null,
    "m1o": null,
    "m2o": null,
    "m3o": null,
    "m4o": null,
    "m5o": null,
    "m6o": null,
    "m7o": null,
    "m8o": null,
    "m9o": null,
    "m10o": null,
    "m11o": null,
    "m12o": null,
    "avg": null,
    "editable": null,
    "isParent": true,
    "locked": false,
    "lockMsg": null,
    "icontype": "OBT_364",
    "leaf": null,
    "dptname": null,
    "dpt_dpt_id": null,
    "is_all_dpt_data": null,
    "attId": null,
    "code": "TEST_PASTATAS_11",
    "alokAggrType": "OTHER"
},
{
    "valId": null,
    "oclOclIdIndexType": null,
    "yearno": null,
    "docId": 275105,
    "iconCls": null,
    "versionNo": 1,
    "parentId": null,
    "id": "275105>1>>823182>4",
    "nameId": "823182",
    "name": "Administracinis pastatas – Naugarduko 3, Vilnius, Vilniaus m. sav.",
    "menesiai": null,
    "ivedimas": null,
    "rowTypeIndex": 1,
    "duomensTipas": null,
    "y": null,
    "m1": null,
    "m2": null,
    "m3": null,
    "m4": null,
    "m5": null,
    "m6": null,
    "m7": null,
    "m8": null,
    "m9": null,
    "m10": null,
    "m11": null,
    "m12": null,
    "yo": null,
    "m1o": null,
    "m2o": null,
    "m3o": null,
    "m4o": null,
    "m5o": null,
    "m6o": null,
    "m7o": null,
    "m8o": null,
    "m9o": null,
    "m10o": null,
    "m11o": null,
    "m12o": null,
    "avg": null,
    "editable": null,
    "isParent": true,
    "locked": false,
    "lockMsg": null,
    "icontype": "OBT_364",
    "leaf": null,
    "dptname": null,
    "dpt_dpt_id": null,
    "is_all_dpt_data": null,
    "attId": null,
    "code": "TEST_PASTATAS_12",
    "alokAggrType": "OTHER"
},
{
    "valId": null,
    "oclOclIdIndexType": null,
    "yearno": null,
    "docId": 275106,
    "iconCls": null,
    "versionNo": 1,
    "parentId": null,
    "id": "275106>1>>823184>5",
    "nameId": "823184",
    "name": "***PASTATAS 13 – Grybų 35, Vilnius, Vilniaus m. sav.",
    "menesiai": null,
    "ivedimas": null,
    "rowTypeIndex": 1,
    "duomensTipas": null,
    "y": null,
    "m1": null,
    "m2": null,
    "m3": null,
    "m4": null,
    "m5": null,
    "m6": null,
    "m7": null,
    "m8": null,
    "m9": null,
    "m10": null,
    "m11": null,
    "m12": null,
    "yo": null,
    "m1o": null,
    "m2o": null,
    "m3o": null,
    "m4o": null,
    "m5o": null,
    "m6o": null,
    "m7o": null,
    "m8o": null,
    "m9o": null,
    "m10o": null,
    "m11o": null,
    "m12o": null,
    "avg": null,
    "editable": null,
    "isParent": true,
    "locked": false,
    "lockMsg": null,
    "icontype": "OBT_364",
    "leaf": null,
    "dptname": null,
    "dpt_dpt_id": null,
    "is_all_dpt_data": null,
    "attId": null,
    "code": "TEST_PASTATAS_13",
    "alokAggrType": "OTHER"
},
{
    "valId": null,
    "oclOclIdIndexType": null,
    "yearno": null,
    "docId": 275630,
    "iconCls": null,
    "versionNo": 1,
    "parentId": null,
    "id": "275630>1>>823760>6",
    "nameId": "823760",
    "name": "Mano pavadinimas 1 – Pilies 1, Vilniaus m.",
    "menesiai": null,
    "ivedimas": null,
    "rowTypeIndex": 1,
    "duomensTipas": null,
    "y": null,
    "m1": null,
    "m2": null,
    "m3": null,
    "m4": null,
    "m5": null,
    "m6": null,
    "m7": null,
    "m8": null,
    "m9": null,
    "m10": null,
    "m11": null,
    "m12": null,
    "yo": null,
    "m1o": null,
    "m2o": null,
    "m3o": null,
    "m4o": null,
    "m5o": null,
    "m6o": null,
    "m7o": null,
    "m8o": null,
    "m9o": null,
    "m10o": null,
    "m11o": null,
    "m12o": null,
    "avg": null,
    "editable": null,
    "isParent": true,
    "locked": false,
    "lockMsg": null,
    "icontype": "OBT_364",
    "leaf": null,
    "dptname": null,
    "dpt_dpt_id": null,
    "is_all_dpt_data": null,
    "attId": null,
    "code": "OBJ_815895",
    "alokAggrType": "OTHER"
}],
"messages": null

}

鉴于我总是知道代码,我需要提取 docId、id、nameId。

假设我有一个代码为 OBJ_815895 的对象。这是我迄今为止尝试过的(正则表达式):

"valId":null,"oclOclIdIndexType":null,"yearno":null,"docId":(.+?),"iconCls":null,"versionNo":1,"parentId":null,"id":"(.+?)","nameId":"(.+?)","name":".+?","menesiai":null,"ivedimas":null,"rowTypeIndex":1,"duomensTipas":null,"y":null,"m1":null,"m2":null,"m3":null,"m4":null,"m5":null,"m6":null,"m7":null,"m8":null,"m9":null,"m10":null,"m11":null,"m12":null,"yo":null,"m1o":null,"m2o":null,"m3o":null,"m4o":null,"m5o":null,"m6o":null,"m7o":null,"m8o":null,"m9o":null,"m10o":null,"m11o":null,"m12o":null,"avg":null,"editable":null,"isParent":true,"locked":false,"lockMsg":null,"icontype":"OBT_364","leaf":null,"dptname":null,"dpt_dpt_id":null,"is_all_dpt_data":null,"attId":null,"code":"OBJ_815895"

起初我认为这是有效的,当我提取列表中的第一个对象时……我承认我对正则表达式不太擅长,如果你能给我任何建议,我将不胜感激。

[答案]:

创建正则表达式提取器:

正则表达式提取器 [代码]

**Reference Name** : code
**Regular Expression** : "code":"(.+?)"
**Template** : $1$
**Match No.** : -1

正则表达式提取器 [docId]

**Reference Name** : docId
**Regular Expression** : "docId":(\d+)
**Template** : $1$
**Match No.** : -1

添加了 groovy 脚本来设置参数:

for(int i = 1; i<vars.get("code_matchNr");i++){
    if(vars.get("code_"+i) == vars.get("objectId")){
        vars.put("docId",vars.get("docId_"+i));
        vars.put("id",vars.get("id_"+i));
        vars.put("nameId",vars.get("nameId_"+i));
    }
}

【问题讨论】:

    标签: regex json jmeter


    【解决方案1】:

    添加Regular Expression Extractors,一个例子:

    • 参考名称:DocId
    • 正则表达式:"docId":"(.*?)"
    • 模板:$1$
    • 比赛编号:-1

    因为匹配号为负数:

    如果匹配数设置为负数,则处理采样器数据中所有可能的匹配。变量设置如下:

    • refName_matchNr - 找到的匹配数;可能是 0
    • refName_n,其中 n = 1,2,3 等 - 由模板生成的字符串
    • refName_n_gm,其中 m=0,1,2 - 匹配 n 的组

    当然也提取代码并在附加脚本中按索引匹配它们。

    【讨论】:

    • 我想你误解了我的意思。我的 json 响应有多个对象,这些对象又具有参数(docId、nameId 等)。我需要从整个 json 响应中的特定对象中提取 docId。示例:数据[{response_code1},{response_code2},{response_code2}]。例如,我需要从 response_code2 中提取 DocId,知道该响应代码。
    • 我不确定我是否关注你。你所说的,会给我所有可能的 DocId,但我不知道哪个 DocId 属于哪个代码。这就是我需要的,根据代码提取数据。
    • 嗯,这不是我想要的方式,但你的回答给出了我需要的结果。用你的答案编辑了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多