4

我有一个关于获取从 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 处理器,但我以不带双引号的冒号和字符串数据 的形式获取提取的数据。

4

1 回答 1

11

我会推荐使用JSR223 PostProcessor而不是这个 JSON Path one。

  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}根据需要参考提取的值

参考:

于 2016-08-01T11:06:57.893 回答