2015-03-25 147 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