使用 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 请求的查询。

我想用双引号和冒号提取 1 个信息块 json(原样)作为第二个 HTTP 请求的一部分。

  {
      "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 路径一。

  1. 将 JSR223 后处理器添加为 returns 以上 JSON
  2. 请求的子项
  3. 在 "Language" 下拉列表中选择 groovy
  4. 将以下代码放入 JSR223 PostProcessor "Script" 区域:

    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);
    
  5. 在需要时将提取的值引用为 ${json}

参考文献: