2016-11-09 117 views
0

我正在通過輸入酒店名稱進行酒店搜索.Below是有效的JSON響應。這是一家酒店搜索的迴應。我已經運行了15個線程,並且針對與此類似的不同酒店,我還有14個JSON響應。在這個迴應中,您可以看到提供者使用數組值的「提供者」和「結果」。我的要求是找出有多少優惠,即每家提供商共有15家酒店。從JSON響應中提取和處理

"providers": 
    [ 
     { 
      "MM_logofile":"agd.svg", 
      "MM_isOfficialWithoutLogo":false, 
      "code":"AGD", 
      "name":"Agoda.com", 
      "logo":"AGD.png", 
      "isOfficial":false 
     }, 
     { 
      "MM_logofile":"ian.svg", 
      "MM_isOfficialWithoutLogo":false, 
      "code":"IAN", 
      "name":"Hotels.com", 
      "logo":"IAN-Other.png", 
      "isOfficial":false 
     }, 
     { 
      "MM_logofile":"gar.svg", 
      "MM_isOfficialWithoutLogo":false, 
      "code":"GAR", 
      "name":"getaroom.com", 
      "logo":"GAR.png", 
      "isOfficial":false 
     }, 
     { 
      "MM_logofile":"exp.svg", 
      "MM_isOfficialWithoutLogo":false, 
      "code":"EXP", 
      "name":"Expedia.dk", 
      "logo":"EXP-DK.png", 
      "isOfficial":false 
     }, 
     { 
      "MM_logofile":"acc.svg", 
      "MM_isOfficialWithoutLogo":false, 
      "code":"ACC", 
      "name":"AccorHotels.com", 
      "logo":"ACC.png", 
      "isOfficial":true 
     }, 

    ], 

    "results": 
    [ 
     { 
      "roomName":"Standard Twin Rm Special Offer - Best price guarantee", 
      "totalRate":918.0, 
      "isCheapestRate":true, 
      "hasFreeCancelation":false, 
      "inclusions": 
      [ 
      ], 
      "availableRooms":null, 
      "providerIndex":0, 
      "includesAllTaxes":false, 
      "excludedCharges": 
      [ 
       0 
      ], 
      "bookUri":"https://72750.api.hotelscombined.com/ProviderRedirect.ashx?key=0.11648360.-378376995.495.USD.123155627&source=202-0&a_aid=72750&brandID=177977" 
     }, 
     { 
      "roomName":"Standard Double Rm Special Offer - Best price guarantee", 
      "totalRate":918.0, 
      "isCheapestRate":false, 
      "hasFreeCancelation":false, 
      "inclusions": 
      [ 
      ], 
      "availableRooms":null, 
      "providerIndex":0, 
      "includesAllTaxes":false, 
      "excludedCharges": 
      [ 
       0 
      ], 
      "bookUri":"https://72750.api.hotelscombined.com/ProviderRedirect.ashx?key=0.11648360.-378376995.496.USD.1523114518&source=202-1&a_aid=72750&brandID=177977" 
     }, 
     { 
      "roomName":"Standard Double Room Hot Deal - Best price guarantee", 
      "totalRate":918.0, 
      "isCheapestRate":false, 
      "hasFreeCancelation":false, 
      "inclusions": 
      [ 
      ], 
      "availableRooms":null, 
      "providerIndex":1, 
      "includesAllTaxes":false, 
      "excludedCharges": 
      [ 
       0 
      ], 
      "bookUri":"https://72750.api.hotelscombined.com/ProviderRedirect.ashx?key=0.11648360.-378376995.497.USD.573302441&source=202-2&a_aid=72750&brandID=177977" 
     }, 
     { 
      "roomName":"Standard Twin Room Hot Deal - Best price guarantee", 
      "totalRate":918.0, 
      "isCheapestRate":false, 
      "hasFreeCancelation":false, 
      "inclusions": 
      [ 
      ], 
      "availableRooms":null, 
      "providerIndex":2, 
      "includesAllTaxes":false, 
      "excludedCharges": 
      [ 
       0 
      ], 
      "bookUri":"https://72750.api.hotelscombined.com/ProviderRedirect.ashx?key=0.11648360.-378376995.498.USD.1523907592&source=202-3&a_aid=72750&brandID=177977" 
     }, 
     { 
      "roomName":"Standard Room, 1 Double Bed", 
      "totalRate":926.2, 
      "isCheapestRate":false, 
      "hasFreeCancelation":false, 
      "inclusions": 
      [ 
      ], 
      "availableRooms":null, 
      "providerIndex":3, 
      "includesAllTaxes":false, 
      "excludedCharges": 
      [ 
       0 
      ], 
      "bookUri":"https://72750.api.hotelscombined.com/ProviderRedirect.ashx?key=0.13476094.-378377052.1210.USD.1325439035&source=202-4&a_aid=72750&brandID=177977" 
     }, 

    ], 
+1

代碼在哪裏? json在哪裏? – Weedoze

+0

嘗試添加部分json,並將建立在 –

+1

我正在做它。我的回覆太長,所以無法發佈。給我一分鐘。 – Jane

回答

1

以下是BeanShell的代碼:

import java.util.HashMap; 
import java.util.Map; 

results_count = Integer.parseInt(vars.get("results_matchNr")); 
providers_count = Integer.parseInt(vars.get("providers_matchNr")); 
log.info("total results " + results_count); 
Map results = new HashMap(); 
Map providers = new HashMap(); 

for(i=1; i<=providers_count; i++){ 
    log.info("iteration " + i); 
    temp = vars.get("providers_"+i); 
    log.info("provider_name " + temp); 
    providers.put(i-1, temp); 
} 
log.info("providers: " + providers); 

int provider_index = -1; 
String provider_name = ""; 

for(i=1; i<=results_count; i++){ 
    log.info("iteration " + i); 
    provider_index = Integer.parseInt(vars.get("results_"+i)); 
    log.info("provider_index " + provider_index); 
    provider_name = providers.get(provider_index); 
    log.info("provider name :" + provider_name); 
    if(results.get(provider_name) == null){ 
     log.info("ading key for the first time " + provider_name); 
     results.put(provider_name, 1); 
    } 
    else{ 
     log.info("second time " + provider_name); 
     int existing = results.get(provider_name); 
     log.info("exisiting value " + existing); 
     int updateValue = existing+1; 
     log.info("updated value: " + updateValue); 
     results.put(provider_name, updateValue); 
    } 
} 

log.info("results-providers mapping " + results); 

int threadNum = ctx.getThreadNum(); 

// if you want to log something to jmeter.log file 

// Pass true if you want to append to existing file othewise false. 
f = new FileOutputStream("G:\\naveen\\mywork\\testing\\performance\\tools\\jmeter\\examples\\result.csv", true); 
p = new PrintStream(f); 
this.interpreter.setOut(p); 
String output = "thread number#" + threadNum + " " + results; 
print(output); 
f.close(); 

添加BeanShell的後處理器採樣,並添加上面的代碼。

enter image description here

添加JSONPATH提取得到的提供商,並添加以下語法:

$.providers[*].name 

enter image description here

添加另一個JSONPATH提取得到的結果/報價,並添加以下語法:

$.results[*].providerIndex 

enter image description here

注意:更改文件位置以根據您的機器在beanshell代碼中存儲結果。

以下是將被保存到文件中的結果:

thread number#0 {Expedia.dk=1, Agoda.com=2, Hotels.com=1, getaroom.com=1} 
thread number#1 {Expedia.dk=1, Agoda.com=2, Hotels.com=1, getaroom.com=1} 

//兩行兩個線程。在這裏,Agoda.com提供商有兩個優惠,所以計數是2,剩下的提供商每個都有一個優惠。

說明:驗證了2個用戶的腳本。結果文件中會出現兩個條目(一個線程 - >一行)。根據您對beanshell代碼(與文件編寫相關的代碼)的要求來調整窗口。

+0

非常感謝你。這是我的預期。是否可以合併來自所有線程的所有結果併爲每個提供商提供優惠的數量?就像在你的例子中,它會是Expedia.dk = 2,Agoda = 4,Hotels.com = 2,getaroom.com = 2 – Jane

+0

我認爲這是不可能的,因爲所有線程並行運行,並且每個線程運行它自己的BeanShell副本PostProcessor中。你必須在執行後執行此操作。 –

+0

好的。感謝Naveen的幫助 – Jane