我也有同样的问题
postman get values from array and set it,dynamically ,on env variables
但我找不到如何为自己修改最佳答案的方法。
JSON:
{
"meta": {
"code": 200,
"requestId": "5c871f361ed2196e43a00982"
},
"response": {
"venues": [
{
"id": "536e7cac498ea9201478f0bb",
"name": "Yeni Camii Alımyeri 1",
"location": {
"lat": 41.06841467513242,
"lng": 40.69481069825992,
"labeledLatLngs": [
{
"label": "display",
"lat": 41.06841467513242,
"lng": 40.69481069825992
}
],
"distance": 60953,
"cc": "TR",
"country": "Türkiye",
"formattedAddress": [
"Türkiye"
]
},
"categories": [
{
"id": "530e33ccbcbc57f1066bbff3",
"name": "Town",
"pluralName": "Towns",
"shortName": "Town",
"icon": {
"prefix": "https://ss3.4sqi.net/img/categories_v2/parks_outdoors/neighborhood_",
"suffix": ".png"
},
"primary": true
}
],
"referralId": "v-1552359222",
"hasPerk": false
},
{
"id": "5a22b75566611634a15d1f68",
"name": "Osmançavuş Mah.",
"location": {
"lat": 40.714043,
"lng": 41.209717,
"labeledLatLngs": [
{
"label": "display",
"lat": 40.714043,
"lng": 41.209717
}
],
"distance": 64990,
"cc": "TR",
"city": "Erzurum",
"country": "Türkiye",
"formattedAddress": [
"Erzurum",
"Türkiye"
]
},
"categories": [
{
"id": "530e33ccbcbc57f1066bbff3",
"name": "Town",
"pluralName": "Towns",
"shortName": "Town",
"icon": {
"prefix": "https://ss3.4sqi.net/img/categories_v2/parks_outdoors/neighborhood_",
"suffix": ".png"
},
"primary": true
}
],
"referralId": "v-1552359222",
"hasPerk": false
},
{
"id": "55e0acd1498e06a1c0bd34ca",
"name": "Ören Beldesi",
"location": {
"lat": 41.17862276665214,
"lng": 40.886770827531386,
"labeledLatLngs": [
{
"label": "display",
"lat": 41.17862276665214,
"lng": 40.886770827531386
}
],
"distance": 78451,
"cc": "TR",
"country": "Türkiye",
"formattedAddress": [
"Türkiye"
]
},
"categories": [
{
"id": "530e33ccbcbc57f1066bbff3",
"name": "Town",
"pluralName": "Towns",
"shortName": "Town",
"icon": {
"prefix": "https://ss3.4sqi.net/img/categories_v2/parks_outdoors/neighborhood_",
"suffix": ".png"
},
"primary": true
}
],
"referralId": "v-1552359222",
"hasPerk": false
},
{
"id": "53e33833498ec6568c338076",
"name": "Mapsino Köy Çıkışı",
"location": {
"lat": 40.81617736816406,
"lng": 40.26142501831055,
"labeledLatLngs": [
{
"label": "display",
"lat": 40.81617736816406,
"lng": 40.26142501831055
}
],
"distance": 34825,
"cc": "TR",
"country": "Türkiye",
"formattedAddress": [
"Türkiye"
]
},
"categories": [
{
"id": "530e33ccbcbc57f1066bbff3",
"name": "Town",
"pluralName": "Towns",
"shortName": "Town",
"icon": {
"prefix": "https://ss3.4sqi.net/img/categories_v2/parks_outdoors/neighborhood_",
"suffix": ".png"
},
"primary": true
}
],
"referralId": "v-1552359222",
"hasPerk": false
}
],
"confident": false
}
我试图获取 response.venues.id 的值,有时我最终不得不手动创建 29 个变量。
如果有人可以告诉我如何编辑此代码以处理我的 JSON 响应,那就太好了。
let idCount = 1
let nameCount = 1
let typeCount = 1
_.each(_.first(pm.response.json()).variables, (arrItem) => {
pm.environment.set(`varID${idCount ++}`, arrItem.id);
pm.environment.set(`varName${nameCount ++}`, arrItem.name);
pm.environment.set(`varType${typeCount ++}`, arrItem.type);
})
最佳答案
您可以用以下内容替换现有脚本:
let responseBody = pm.response.json(),
venues = _.get(responseBody, 'response.venues');
_.forEach(venues, (venue, index) => {
pm.environment.set(`varID${++index}`, venue.id);
});
这会将您所有的 ID 作为变量存储在环境中,它会像这样显示:
解释:
首先,我们从您的响应主体中获取 venues
以便轻松地在其上运行循环。
然后我们只需对数组中的项目运行一个简单的 forEach 循环,并将每个数组项目的相应 ID 存储在环境变量中。
我们正在使用 lodash library
对于像 _.get 和 _.forEach
这样内置于 postman 中的函数。
关于javascript - postman :从 JSON 数组中获取数组值并将它们设置为 postman 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55113671/