postman 如何在post man中正确解析这个响应?

mdfafbf1  于 2023-01-26  发布在  Postman
关注(0)|答案(1)|浏览(168)

我试过在postman中使用下面的代码来解析一个数组响应(下面更详细)。由于某种原因,我没有得到id值。无论我输入的数组位置是什么,我都会得到一个类型错误
无法读取未定义的属性(阅读“0”)
Postman 手稿

const response = pm.response.json();
console.log(pm.globals.get("alarm1Id", response.result.alarms[0].id));

获取请求响应:

{
    "status": "success",
    "result": {
        "alarms": [
            {
                "id": "C6707LJmR1yTzjYrfoBTxw",
                "name": "Test Alarm1",
                "state": "PURGED",
                "isAssignedToCurrentUser": false,
                "timeOfLastStateChange": "2023-01-20T01:04:38.917Z",
                "type": "NORMAL",
                "associatedCameras": [
                    "4xIx1DMwMLSwMDU2sTA2NdNLTsw1MBASmCLZ_fximWvi_v08agen8bMBAA"
                ],
                "priority": 0,
                "isNoteRequired": false,
                "missedTriggers": 0,
                "timeOfMostRecentAcknowledgement": "2023-01-20T01:02:09.670Z",
                "timeOfMostRecentActivation": "2023-01-20T01:02:02.346Z",
                "timeOfMostRecentPurge": "2023-01-20T01:04:38.917Z",
                "durationOfPreRecording": "PT5S",
                "durationOfPostRecording": "PT1M",
                "digitalOutputsToActivate": []
            },
            {
                "id": "kNJnCVsdQFWzvOeBshTqeg",
                "name": "FR",
                "state": "PURGED",
                "isAssignedToCurrentUser": false,
                "timeOfLastStateChange": "2022-12-01T23:26:41.482Z",
                "type": "NORMAL",
                "associatedCameras": [
                    "49Ix0UsyNU4ySEpN002yTDbSNTFONNW1NLEw001LTkw0NjI2ME-0sNBLTsw1MBASmCLZ_fximWvi_v08agen8bMBAA"
                ],
                "priority": 0,
                "isNoteRequired": false,
                "missedTriggers": 0,
                "timeOfMostRecentAcknowledgement": "2022-07-07T18:21:28.981Z",
                "timeOfMostRecentActivation": "2022-07-06T17:17:09.751Z",
                "timeOfMostRecentPurge": "2022-12-01T23:26:41.482Z",
                "durationOfPreRecording": "PT5S",
                "durationOfPostRecording": "PT1M",
                "digitalOutputsToActivate": []
            }
        ]
    }
}

我试过不同的列表位置,我想我只是没有完全理解响应的数据结构。

xghobddn

xghobddn1#

response = JSON.parse(responseBody)

alarm1Id =响应.结果.报警[0].id
我做了一些挖掘,我能够使用上面的脚本提取ID数据

相关问题