正则表达式提取 [Jmeter]

Regex Extract [Jmeter]

简而言之:我有一个 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** : $
**Match No.** : -1

正则表达式提取器 [docId]

**Reference Name** : docId
**Regular Expression** : "docId":(\d+)
**Template** : $
**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));
    }
}

添加Regular Expression Extractors,一个例子:

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

因为负匹配号:

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

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

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