2016-08-01 47 views
0

我有一個關於獲取從jmeter中的第一個請求獲得的json響應的一部分以形成一個新的HTTP請求的查詢。用Jmeter解析JSON以傳遞一個JSON數組,因爲它從之前的HTTP響應傳遞到一個新的HTTP請求

我想用雙引號和冒號作爲第二個HTTP請求的一部分提取1塊信息json(原樣)。

{ 
     "details": [ 
     { 
      "outBound": [ 
      { 
       "info": { 
       "date": "2016-08-11", 
       "class": "M", 
       "code": 70, 
       "pricing": [ 
        { 
        "totalAmount": 68.8, 
        "totalTaxAmount": 30.8, 
        "baseFareAmount": 38.0 
        } 
       ], 
       "totalAmount": 68.8, 
       "totalDuration": 160, 
      "referenceNumber": 1, 
      "type": "RP", 
      "id": 1 
      }, 
      "segments": [ 
      { 
      "date": "2016-08-11", 
      "className": "Standard (W)", 
      "code": 70, 
      "totalAmount": 68.8, 
      "totalDuration": 160, 
      "referenceNumber": 1, 
      "type": "RP", 
      "duration": 160, 
      "number": "100" 

      } 
      ] 
     }, 
     { 
      "info": { 
      "date": "2016-08-11", 
      "class": "M", 
      "code": 70, 
      "pricing": [ 
       { 
       "totalAmount": 78.8, 
       "totalTaxAmount": 40.8, 
       "baseFareAmount": 38.0 
       } 
      ], 
      "totalAmount": 78.8, 
      "totalDuration": 160, 
      "referenceNumber": 2, 
      "type": "RP", 
      "id": 2 
      }, 
      "segments": [ 
      { 
      "date": "2016-08-11", 
      "className": "Standard (W)", 
      "code": 70, 
      "totalAmount": 78.8, 
      "totalDuration": 160, 
      "referenceNumber": 2, 
      "type": "RP", 
      "duration": 160, 
      "number": "200" 

      }, 
      { 
      "date": "2016-08-11", 
      "className": "Standard (W)", 
      "code": 70, 
      "totalAmount": 78.8, 
      "totalDuration": 160, 
      "referenceNumber": 2, 
      "type": "RP", 
      "duration": 160, 
      "number": "100" 

      } 
      ] 
     } 
     ], 
     "resultCount": { 
     "count1": 1, 
     "count2": 1 
     }, 
     "displayCount": 2 
    } 
    ] 
    } 

>Expected Output: 
    { 
      "info": { 
      "date": "2016-08-11", 
      "class": "M", 
      "code": 70, 
      "pricing": [ 
       { 
       "totalAmount": 68.8, 
       "totalTaxAmount": 30.8, 
       "baseFareAmount": 38.0 
       } 
      ], 
      "totalAmount": 68.8, 
      "totalDuration": 160, 
      "referenceNumber": 1, 
      "type": "RP", 
      "id": 1 
      }, 
      "segments": [ 
      { 
      "date": "2016-08-11", 
      "className": "Standard (W)", 
      "code": 70, 
      "totalAmount": 68.8, 
      "totalDuration": 160, 
      "referenceNumber": 1, 
      "type": "RP", 
      "duration": 160, 
      "number": "100", 

      } 
      ] 
     } 

我嘗試使用JSON PATH後處理器,但我正在代替結腸和字符串數據中的=形式所提取的數據,而無需雙引號。

回答

1

我會推薦使用JSR223 PostProcessor而不是這個JSON路徑之一。

  1. 添加JSR223 PostProcessor中作爲返回上述JSON請求的孩子
  2. 在「語言」下拉列表中選擇groovy
  3. 將下面的代碼放到JSR223 PostProcessor中「腳本」區域:

    import groovy.json.JsonOutput 
    import groovy.json.JsonSlurper 
    
    def jsonSlurper = new JsonSlurper(); 
    def response = jsonSlurper.parseText(prev.getResponseDataAsString()); 
    def json = JsonOutput.toJson(response.details[0].outBound[0]); 
    
    vars.put("json", json); 
    
  4. 參閱提取的值作爲${json}在需要

參考文獻:

+0

德米特里·喜..謝謝你的回答。它工作正常....但我無法維持提取的json的順序,是否可以按照我需要維護順序的順序提取1塊信息json(因爲它是)第二個HTTP請求。 ?提前致謝。 – Chaitra

相關問題