JMeter提取响应数据中指定的id=xxx中的propList字段中的id值,并设为变量

最近在做自动化测试中遇到了提取字段问题,可能我是第一次遇到,经过各大搜索也没搜索到想要的结果,最终还是同ChatGPT来实现了我问题,以下解决过程。


问:假设提取的响应数据结构如下,提取指定的id=25153中的propList字段中的id值,并设为变量,请在jmeter中实现


{
    "resultCode": "0",
    "resultMsg": "success",
    "data": [
        {
            "id": 25152,
            "categoryId": 48529,
            "name": "标关键",
            "source": 3,
            "propList": [
                {
                    "id": 81529,
                    "propId": 25152,
                    "name": "关键值1",
                    "type": 3,
                    "updateTime": 1683773006584
                },
                {
                    "id": 81530,
                    "propId": 25152,
                    "name": "关键值2",
                    "type": 3,
                    "updateTime": 1683773006593
                }
            ],

            "isSelect": 1,
            "type": 3,

            "projectId": null
        },
        {
            "id": 25153,
            "categoryId": 48529,
            "name": "标否定",

            "source": 3,
            "propList": [
                {
                    "id": 81531,
                    "propId": 25153,
                    "name": "固定1",
                    "type": 3
                },
                {
                    "id": 81532,
                    "propId": 25153,
                    "name": "固定2",
                    "type": 3

                }
            ],
            "type": 3,
            "projectId": null
        }
    ]
}

ChatGPT回答:

方式一:

要在 JMeter 中提取指定 ID 值中的 propList 字段中的 id 值,可以使用 JSON 提取器。首先,您需要添加一个 HTTP 请求,并将响应数据存储在一个变量中。然后,您可以添加一个 JSON 提取器,配置如下:

- 变量名称:propId
- JSON 路径表达式:$.data[?(@.id==25153)].propList[*].id
- 默认值:未找到

这将提取 ID 为 25153 的项目的 propList 字段中的所有 ID 值,并将其存储在名为 propId 的变量中。


方式二:

好的,我可以使用 JMeter 中的 BeanShell 脚本来提取所需的值。首先,我需要添加一个正则表达式提取器来从响应中提取 JSON 数据。然后,我可以使用以下 BeanShell 脚本来提取所需的值:

```
import org.json.JSONArray;
import org.json.JSONObject;

String responseData = vars.get("responseData"); // 获取响应数据
JSONObject jsonObject = new JSONObject(responseData); // 将响应数据转换为 JSON 对象
JSONArray dataArray = jsonObject.getJSONArray("data"); // 获取 data 数组
JSONObject targetObject = null;
for (int i = 0; i < dataArray.length(); i++) {
    JSONObject dataObject = dataArray.getJSONObject(i);
    int id = dataObject.getInt("id"); // 获取 ID 值
    if (id == 25153) { // 如果 ID 值匹配
        targetObject = dataObject;
        break;
    }
}
JSONArray propListArray = targetObject.getJSONArray("propList"); // 获取 propList 数组
JSONArray idArray = new JSONArray(); // 创建一个空的 JSON 数组
for (int i = 0; i < propListArray.length(); i++) {
    JSONObject propObject = propListArray.getJSONObject(i);
    int id = propObject.getInt("id"); // 获取 ID 值
    idArray.put(id); // 将 ID 值添加到 JSON 数组中
}
vars.put("propId", idArray.toString()); // 将 JSON 数组转换为字符串,并将其存储在变量中
```

这将从响应数据中提取 JSON 数据,然后查找 ID 值为 25153 的项目,并提取其 propList 数组中的所有 ID 值。最终,它将把这些 ID 值存储在名为 propId 的变量中。

通过以上方式实现了我的需求,然而通过各种搜索来的结果确没有一个成功的,也可能是我搜索的有问题,欢迎大家留言讨论! 


PS:直接提问没有给出数据结构参照只会回答使用正则表达式,而且通过各种搜索也是回答用的正则表达式,实际使用测试结果失败,所以会点代码或者熟悉JSON提取规则更好!

使用正则表达式提取器,步骤如下:

  1. 添加HTTP请求和正则表达式提取器到测试计划中。
  2. 在HTTP请求中设置响应数据的变量名为response。
  3. 在正则表达式提取器中设置如下属性:
  • 变量名:propId
  • 正则表达式:"propList":[{"id":(\d+)
  • 模板:$1$
  • 匹配号:-1
  1. 在接下来的测试中可以使用${propId}来引用该变量。

—The End—
后续有好方法还会继续更新,欢迎大家一起交流互助。
如果对你有帮助请关注哦!
 

猜你喜欢

转载自blog.csdn.net/github_35856054/article/details/130927771