gpt4 book ai didi

json - 使用 jmeter 解析 JSON 以将 json 数组从先前的 HTTP 响应传递到新的 HTTP 请求

转载 作者:行者123 更新时间:2023-12-04 00:38:04 24 4
gpt4 key购买 nike

我有一个关于获取从 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 Processor,但我以 = 的形式获取提取的数据代替没有双引号的冒号和字符串数据。

最佳答案

我建议使用 JSR223 PostProcessor而不是这个 JSON 路径一。

  • 添加 JSR223 PostProcessor 作为返回高于 JSON 的请求的子级
  • 选择 groovy在“语言”下拉菜单中
  • 将以下代码放入 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);
  • 将提取的值称为 ${json}需要时

  • 引用:
  • Parsing and producing JSON - 学习如何在 Groovy 中使用 JSON
  • Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! - groovy 引擎安装指南(适用于 JMeter < 3.0)和 JMeter 最佳实践中的 Groovy 脚本
  • 关于json - 使用 jmeter 解析 JSON 以将 json 数组从先前的 HTTP 响应传递到新的 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38695999/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com