如何在Postman中从JSON响应中提取值?
我有一个JSON响应:如何在Postman中从JSON响应中提取值?,postman,web-api-testing,Postman,Web Api Testing,我有一个JSON响应: [{ "dateTime": 1603650600000, "supplierName": "Mr Orange GP ", "gender": "male", "supplierId": "788", "appointmentId": 454687, "t
[{
"dateTime": 1603650600000,
"supplierName": "Mr Orange GP ",
"gender": "male",
"supplierId": "788",
"appointmentId": 454687,
"tentativeAppointmentCode": "5f8ff4a8a9d5f"
}, {
"dateTime": 1603650600000,
"supplierName": "Mr Kennedy test ",
"gender": "male",
"supplierId": "600",
"appointmentId": 573993,
"tentativeAppointmentCode": "5f8ff4a8a9d5f"
}, {
"dateTime": 1603651500000,
"supplierName": "Mr Orange GP ",
"gender": "male",
"supplierId": "788",
"appointmentId": 454688,
"tentativeAppointmentCode": "5f8ff4a8a9d5f"
}, {
"dateTime": 1603651500000,
"supplierName": "Mr Kennedy test ",
"gender": "male",
"supplierId": "600",
"appointmentId": 573994,
"tentativeAppointmentCode": "5f8ff4a8a9d5f"
}]
我需要提取第一次出现的dateTime
、AppointmentId
和tentiveAppointmentCode的值。
尝试了下面提到的代码,但在本例中无效
var jsonData=JSON.parse(responseBody);
postman.setEnvironmentVariable("dateTime",jsonData.dateTime);
如何提取这些值并存储在变量中,以便在其他请求中使用它?我已经做了类似的事情来存储令牌以供以后使用
const jsonData = pm.response.json();
pm.environment.set("token", jsonData.access_token);
假设“预订ID”的意思是tentiveAppointCode
(预订ID不会出现在您的响应正文中),则以下操作将完成此操作:
let jsonData = pm.response.json();
pm.environment.set("dateTime",jsonData[0].dateTime);
pm.environment.set("appointmentId",jsonData[0].appointmentId);
pm.environment.set("tentativeAppointmentCode",jsonData[0].tentativeAppointmentCode);
我还建议使用
pm.response.json()
而不是json.parse(responseBody)
,不要使用postman.setEnvironmentVariable
,而是使用较新的pm.environment.set()
语法。你说得对,我只是不假思索地从问题中抄袭过来,谢谢你指出这一点。更新了答案。