从 webhook 解析 json

Parsing json from webhook

我正在尝试使用 Quickbooks 设置 webhook,为了测试,我正在使用 JSON 他们将 post 到 webhook 的示例。

JSON的格式如下:

{
    "eventNotifications":[
    {
        "realmId":"1185883450",
        "dataChangeEvent":
        {
            "entities":[
            {
                "name":"Customer",
                "id":"1",
                "operation":"Create",
                "lastUpdated":"2015-10-05T14:42:19-0700"
            },
            {
                "name":"Vendor",
                "id":"1",
                "operation":"Create",
                "lastUpdated":"2015-10-05T14:42:19-0700"
            }]
        }
    }]
}

使用 Javascript,我尝试以多种方式解析它,但我一直卡在实体部分。

尝试 1: 通过这次尝试,我得到一个长度未定义的错误,这让我尝试下一次尝试。

for (var i = 0; i < req.body.eventNotifications.length; i++) {

    // Works fine
    realmId.push(req.body.eventNotifications[i].realmId);

    for (var j = 0; j < req.body.eventNotifications[i].dataChangeEvent.entities.length; j++) {
        // length is undefined
    }
}

尝试 2: 我没有收到上述错误,但我尝试在实体下访问的任何内容都是未定义的。

for (var i = 0; i < req.body.eventNotifications.length; i++) {

    // Works fine
    realmId.push(req.body.eventNotifications[i].realmId);

    for (var j = 0; j < req.body.eventNotifications[i].dataChangeEvent.length; j++) {
        // Anything I try to access is undefined
        var test = req.body.eventNotifications[i].dataChangeEvent[j].entities.name;
    }
}
for (var i = 0; i < req.body.eventNotifications.length; i++) {

    // Works fine
    realmId.push(req.body.eventNotifications[i].realmId);

    var eventNotification = req.body.eventNotifications[i];

    for (var j = 0; j < eventNotification.dataChangeEvent.entities.length; j++) {
        var entityName = eventNotification.dataChangeEvent.entities[j].name;
    }
}