在Postman中使用该服务时,我在响应正文中获得了多个块,我需要获取一个元素值并将其存储在Environment变量中,以便在下一个请求中使用它。
请帮助获取depositInfoId
的值,并需要在环境变量中进行设置。
我尝试过以下一种,但没有用。
pm.environment.set("depositInfoId",pm.response.json().depositInfoList);
pm.environment.set("depositInfoId",pm.response.json().depositInfoList.depositInfoId);
下面是带有几个块的示例响应正文:
{
"qualStatus": "PASS",
"blackList": {
"blackListStatus": "PASS",
"blackListStatusDesc": "CustomerName Match Not Found."
},
"fraudCheck": {
"linesActivated": null,
"status": "NA",
"activationDetails": null,
"errorDetails": null,
"creditCheck": {
"intlDialingEligibilityIndicator": true,
"intlRoamingEligibilityIndicator": true,
"creditCheckDetails": [{
"linesApproved": 1,
"approvedTerm": "24",
"accountSpendingLimit": null,
"lineDepositAmount": null,
"securityDepositeAvailableIndicator": null,
"creditLimitProgramIndicator": null,
"noDeviceProgramIndicator": null,
"creditOptionId": null
}],
"status": "APPROVED",
"errorDetails": null,
"accountNumber": "12345678",
"creditAssessmentID": "123456",
"contractTypes": null,
"esimProductCode": null
}
},
"depositInfoList": [{
"depositInfoId": 863,
"transactionID": "123456",
"lineDepositAmount": null,
"linesActivated": "1",
"internationalDailingDeposit": null,
"approvedTerm": "24",
"depositSubscriberCount": null,
"spendingLimit": null,
"creditAssessmentId": "123456",
"securityDepositAvailable": null,
"creditLimitIndicator": null,
"noDeviceProgramIndicator": null,
"creditOptionId": null
}]
}
在我使用第一个代码时,环境变量值存储如下。
depositInfoId - [object Object] - [object Object]
当我使用第二个代码时,字段中没有存储空白。
答案 0 :(得分:1)
如果要将整个对象/数组存储在Environment / Global / Collection变量中,则需要首先对值进行字符串化:
const jsonBody = pm.response.json();
pm.environment.set('depositInfoList', JSON.stringify(jsonBody.depositInfoList)); //will store the whole array as string
pm.environment.set('depositInfoItem', JSON.stringify(jsonBody.depositInfoList[0])); //will store first object from the array as string
pm.environment.set('depositInfoItemId', jsonBody.depositInfoList[0].depositInfoId); //will store if of first object from the array
否则,您将存储[object Object]值:
const jsonBody = pm.response.json();
pm.environment.set('depositInfoList', jsonBody.depositInfoList); //will store [object Object]
pm.environment.set('depositInfoItem', jsonBody.depositInfoList[0]); //will store [object Object]
pm.environment.set('depositInfoItemId', jsonBody.depositInfoList[0].depositInfoId); //will store if of first object from the array
第二,如果要从Environment / Global / Collection变量获取值并将其用作数组/对象,则需要将其解析为JSON:
const depositInfoList = JSON.parse(pm.environment.get('depositInfoList')); //returns array
const depositInfoItem = JSON.parse(pm.environment.get('depositInfoItem')); //returns object
const depositInfoItemId = pm.environment.get('depositInfoItemId'); //returns number
答案 1 :(得分:0)
pm.response.json().depositInfoList
这会将整个depositInfoList JSON数据保存到环境变量,因此当您尝试访问它时,它会显示Object Object。
pm.response.json().depositInfoList.depositInfoId
这没有意义,因为depositInfoList
是一个数组,当您想从数组中获取值时必须提供索引。
pm.response.json().depositInfoList[0].depositInfoId
这应该可行,因为您试图获取数组depositInfoList
的第一条记录的属性值。