Jmeter聚合来自json的某些响应值以在下一个请求中使用

时间:2015-04-02 21:50:45

标签: json jmeter

我有来自请求的以下回复

[
{
    "id": 3767,
    "sellerName": "abc",
    "siteActivity": [
        {
            "siteId": -1,
            "siteName": "example.com",
            "categories": [
                {
                    "categoryId": 79654,
                    "parentId": null,
                    "name": "Photo & Picture Frames",
                    "siteName": null,
                    "channelType": null
                },
                {
                    "categoryId": 114397,
                    "parentId": null,
                    "name": "Chests of Drawers",
                    "siteName": null,
                    "channelType": null
                },
                {
                    "categoryId": 11707,
                    "parentId": null,
                    "name": "Jewellery Boxes",
                    "siteName": null,
                    "channelType": null
                },
                {
                    "categoryId": 45505,
                    "parentId": null,
                    "name": "Serving Trays",
                    "siteName": null,
                    "channelType": null
                }
            ]
        }
    ]
},
{
    "id": 118156,
    "sellerName": "xyz",
    "siteActivity": [
        {
            "categoryId": 45505,
            "parentId": null,
            "name": "Serving Trays",
            "siteName": null,
            "channelType": null
        }
    ]
}
]

现在,我需要提取“id”值和“categoryId”值,并将它们作为列表发送到下一个请求体中。

目前,我正在使用带有表达式

的JSON Path Extractor
    $.[*].id

了解所有ID并

    $.[*].siteActivity.[categoryId]

类别ID。 接下来,我想使用上面的值并将它们作为请求体中的参数发送。 目前,我只能使用

提取一个ID
   $.[0].id

然后将其分配给变量“id”并在请求正文中使用以下内容

 {"ids":[{"id":"${id}"}]}

但我希望能够发送

 {"ids":[{"id":"${id}"},{"id":"${id2}"}....]}

对于有多少ID没有限制,所以我不能硬编码并需要动态的东西来进行聚合。什么样的处理器可以帮助我?如果可以,请添加一些示例。

1 个答案:

答案 0 :(得分:1)

我相信您应该可以使用Beanshell PostProcessor来构建请求。

根据您的示例数据,您的$.[*].id JSONPath表达式应返回以下值:

id=[3767,118156]
id_1=3767
id_2=118156

所以基本上你需要:

  1. 确定" id"计数
  2. 填充要发送的动态JSON对象
  3. 将其存储到JMeter变量供以后使用
  4. 为了做到这一点,在 JSONPath Extractor之后添加一个Beanshell PostProcessor ,并将以下代码放入其"脚本"区域

    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject; // necessary imports
    
    JSONObject data2Send = new JSONObject();
    JSONArray array = new JSONArray(); // instantiate JSON-related classes
    
    int idCount = vars.get("id").split(",").length; // get "id" variables count
    
    for (int i = 1; i <= idCount; i++) { // for each "id" variable
        JSONObject id = new JSONObject(); // construct a new JSON Object
        id.put("id", vars.get("id_" + i));// with name "id" and value id_X 
        array.add(id);  // add object to array
    } 
    
    data2Send.put("ids",array); // add array to "ids" JSON Object
    vars.put("myJSON", data2Send.toString()); // store value as "myJSON" variable
    

    您可以根据需要将{"ids":[{"id":"3767"},{"id":"118156"}]}数据称为${myJSON}

    该方法适用于任何数量的&#34; id&#34;变量。

    参考文献: