我試圖通過從回應正文中選擇 id 來使用預請求腳本在郵遞員中設定 2 個集合變數。有兩個 id,即 id 和 subId,只有當 id 鏈接到 subId 時,我才需要在集合變數中設定這兩個 id。
需要從下面的 json 回應中獲取 id 和 subId(可能有多個記錄,其中 id 沒有 subId 值)。請幫我解決這個問題。
{
"result": [
{
"id": 26,
"name": "Testing",
"code": "TST-012",
"branches": [
{
"emailId": null,
"country": {
"shortName": "Niu",
"currency": "New Zealand Dollar"
}
}
],
"subId": [
{
"id": 46,
"name": "qa",
"code": "qa"
}
]
},
{
"id": 27,
"name": "Testing",
"code": "TST-012",
"branches": [
{
"emailId": null,
"country": {
"shortName": "US",
"currency": "US Dollar"
}
}
],
"subId": null
}
]
}
uj5u.com熱心網友回復:
id包含subId非空的提取
const res = pm.response.json();
const matchEle = res.result.find(e => e.subId !== null);
pm.collectionVariables.set("id", matchEle.id); //26
id內部提取subId
- 如果獲取所有 id
const subIds = _.map(matchEle.subId, _.property("id"));
pm.collectionVariables.set("subIds", JSON.stringify(subIds)); //[46]
- 如果只得到第一個 id
pm.collectionVariables.set("subId", matchEle.subId[0].id); //46
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/346591.html
下一篇:為航點位置和鏈接創建json
