2015-03-25 75 views
0

简而言之:我有一个json响应,我需要从中提取一些参数。 (利用JMeter,正则表达式提取)正则表达式提取[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)); 
    } 
} 

回答

1

添加Regular Expression Extractors,一个例子:

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

由于该否定匹配号码:

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

  • refName_matchNr - 找到的匹配数量;可以是0
  • refName_n,其中n = 1,2,3等 - 的基团为匹配Ñ

- 串由模板

  • refName_n_gm,其中m = 0,1,2所产生但是,当然也要提取代码并在其他脚本中通过索引匹配它们。

  • +0

    我想你误会了我。我的JSON响应有多个对象,它们又有参数(docId,nameId等)。我需要从整个json响应的特定对象中提取docId。例如:data [{response_code1},{response_code2},{response_code2}]。例如,我需要从response_code2中提取DocId,并知道响应代码。 – CrazySabbath 2015-03-25 09:32:07

    +0

    我不知道我跟着你。你说什么,会给我所有可能的DocId,但我不知道哪个DocId属于哪个代码。这就是我需要的,以基于代码提取数据。 – CrazySabbath 2015-03-25 09:46:27

    +0

    好吧,这不是我想要的,但你的答案给出了我需要的结果。编辑我的问题与你的答案稍作编辑。 – CrazySabbath 2015-03-25 11:54:26