0
我正在嘗試設置與Quickbooks的webhook和測試我使用JSON的示例,他們將發佈到webhook。從webhook解析json
的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;
}
}
完美!您能否簡要說明爲什麼/如何運作? –
第一個循環遍歷'eventNotifications'數組,爲了便於閱讀,我在變量'eventNotification'中存儲了要在第二個循環中使用的每個'eventNotifications'對象。第二個循環遍歷'entitities'數組,您可以直接訪問它,因爲'dataChangeEvent'是一個對象:'eventNotification.dataChangeEvent.entities'。希望能幫助到你 :) –