簡體   English   中英

使用 ASPJSON 遍歷 JSON

[英]Looping through JSON using ASPJSON

我正在使用 Classic ASP 和 ASPJSON ( http://www.aspjson.com/ ) 來嘗試遍歷通過 Mandrill Webhook 返回的 JSON 測試數據。

這是示例 JSON 數據 - 我意識到第二個塊與第一個塊相同,但我需要使用它,因為當我在實時系統上執行此操作時,webhook 數據將在單個 JSON 文件/帖子中分批返回。

{
  "event":"hard_bounce",
  "msg":{
     "ts":1365109999,
     "subject":"This an example webhook message",
     "email":"example.webhook@mandrillapp.com",
     "sender":"example.sender@mandrillapp.com",
     "tags":[
        "webhook-example"
     ],
     "state":"bounced",
     "metadata":{
        "user_id":111
     },
     "_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa",
     "_version":"exampleaaaaaaaaaaaaaaa",
     "bounce_description":"bad_mailbox",
     "bgtools_code":10,
     "diag":"smtp;550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces."
  },
  "_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa",
  "ts":1433940242
},
{
  "event":"hard_bounce",
  "msg":{
     "ts":1365109999,
     "subject":"This an example webhook message",
     "email":"example.webhook@mandrillapp.com",
     "sender":"example.sender@mandrillapp.com",
     "tags":[
        "webhook-example"
     ],
     "state":"bounced",
     "metadata":{
        "user_id":111
     },
     "_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa1",
     "_version":"exampleaaaaaaaaaaaaaaa",
     "bounce_description":"bad_mailbox",
     "bgtools_code":10,
     "diag":"smtp;550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces."
  },
  "_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa1",
  "ts":1433940242
}

在我的測試 ASP 頁面中,我嘗試了一個簡單的測試(我已經通過將其保存到數據庫並檢查內容來確認 JSON 數據是有效的)

<!--#INCLUDE file="aspJSON.asp" -->
str2 = <POST DATA FROM MANDRILL>
Set oJSON = New aspJSON
oJSON.loadJSON(str2)

Response.Write oJSON.data("event") & "<hr>"
response.write "<h1>test</h1>"

For Each phonenr In oJSON.data("msg")
    Set this = oJSON.data("msg").item(phonenr)
    Response.Write _
    this.item("subject") & ": " & _
    this.item("diag") & "<br>"
Next

Mandrill調用ASP頁面時,返回這個錯誤:

描述:對象不是集合:。

對於這一行: For Each phonenr In oJSON.data("msg")

我一直在研究如何為每個事件循環遍歷它並從“msg”和“_id”值中獲取屬性。

包裝 str2 使其成為有效的 JSON 集合並更新您的代碼以迭代該集合,如下所示。

<!--#INCLUDE file="aspJSON.asp" -->
str2 = <POST DATA FROM MANDRILL>

' Wrap str2 to turn it into a collection
str2 = "{""events"":[" & str2 & "]}"

Set oJSON = New aspJSON
oJSON.loadJSON(str2)

response.write "<h1>test</h1>"

For Each eventrec In oJSON.data("events") ' Iterate through events
    Set this = oJSON.data("events").item(eventrec).item("msg") ' select msg in each eventrec
    Response.Write _
    this.item("subject") & ": " & _
    this.item("diag") & "<br>"
Next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM