我有一个GET请求,该请求返回一个正文数组:
[
{
"id": "79ad1daf-54f0-49cf-8628-7fb38457fdd1",
"deviceId": "wowSomeRandomName",
"deviceName": "wowSomeRandomName",
"iconUrl": "/i/devicepics/smart_tv.svg",
"lastUse": "2018-12-13T10:05:00.609Z"
}
]
我需要将这些变量设置为全局变量。我了解如何对数组外的外部实体执行此操作:
pm.environment.set("deviceUUID", pm.response.json().id);
pm.environment.set("deviceId", pm.response.json().deviceId);
pm.environment.set(. . . .);
and etc.
但是当它们在[]中时该怎么办?
答案 0 :(得分:2)
我认为您可以先将响应转换为JSON,然后对其进行操作:
var responseData = JSON.parse(response);
pm.environment.set("deviceUUID", responseData[0].id);
pm.environment.set("deviceId", responseData[0].deviceId);
and etc
从以下方面得到了这个想法: https://learning.getpostman.com/docs/postman/scripts/test_examples/
请告诉我这是否无效。