我收到来自请求的以下回复
[
{
"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。接下来,我要使用以上值并将它们作为参数发送到请求正文中。目前,我只能使用
$.[0].id
然后将其分配给变量“ id”,并在请求正文中使用以下内容
{"ids":[{"id":"${id}"}]}
但我希望能够发送
{"ids":[{"id":"${id}"},{"id":"${id2}"}....]}
对可以有多少个ID没有限制,因此我无法进行硬编码,并且需要动态的东西来进行聚合。什么样的处理器可以帮助我?如果可以的话,请添加一些示例。
我相信您应该能够使用Beanshell PostProcessor来构建请求。
给定示例数据,您的$.[*].id
JSONPath表达式应返回以下值:
id=[3767,118156]
id_1=3767
id_2=118156
因此,基本上,您需要:
为了这样做,在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}
需要引用您的数据。
该方法将适用于任意数量的“ id”变量。
参考:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句