我需要将响应数据(下面)中的值存储在变量中。
回复正文:
{
"data": {
"packages": [
{
"object": "AAA",
"id": "BBB",
"code": "123",
"name": "Test8",
"description": "Test111",
"fee": "130.00 bath",
"productSeq": "3"
}, {
"object": "AAA",
"id": "CCC",
"code": "456",
"name": "Test9",
"description": "Test222",
"fee": "80.00 bath",
"productSeq": "2"
}, {
"object": "AAA",
"id": "CCC",
"code": "789",
"name": "Test10",
"description":"Test111",
"fee": "70.00 bath",
"productSeq": "1"
}
]
},
"resultCode": "20000",
"resultDesc": "Success",
"developerMessage": "Success"
}
如果响应中对象的id
值是“CCC”,我需要将这样的数据存储在变量中:
{
"object": "AAA",
"id": "CCC",
"code": "456",
"name": "Test9",
"description": "Test222",
"fee": "80.00 bath",
"productSeq": "2"
}, {
"object": "AAA",
"id": "CCC",
"code": "789",
"name": "Test10",
"description":"Test111",
"fee": "70.00 bath",
"productSeq": "1"
}
你能帮我创建那样做的脚本吗?
答案 0 :(得分:0)
你可以这样做: -
var body = JSON.parse(responseBody)
var objArray = [];
for (i=0; i<body.data.packages.length;i++){
if (body.data.packages[i].id === "CCC"){
objArray.push(body.data.packages[i]);
}
}
postman.setEnvironmentVariable("varName", objArray)