簡體   English   中英

使用jmeter解析JSON以傳遞json數組,因為它是從先前的HTTP響應到新的HTTP請求

[英]Parsing JSON with jmeter to pass a json array as it is from previous HTTP response to a new HTTP Request

我有一個關於獲取從jmeter中的第一個請求獲得的json響應的一部分以形成新的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 POST處理器,但我以=的形式獲取提取的數據,而不是雙引號的冒號和字符串數據。

我建議使用JSR223 PostProcessor而不是這個JSON Path。

  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}

參考文獻:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM