Looping through JSON using ASPJSON

2019-06-09 18:08发布

问题:

I am using Classic ASP and ASPJSON (http://www.aspjson.com/) to try to loop through JSON test data returned via a Mandrill Webhook.

This is the sample JSON data - I realise the 2nd block is the same as the first, but I need to use it since when I do this on the live system the webhook data will be returned in batches in a single JSON file / post.

{
  "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
}

In my test ASP page, I tried a simple test (I have already confirmed the JSON data is valid by saving it to a database and checking the content)

<!--#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

When Mandrill calls the ASP page, it returns this error:

Description: Object not a collection : .

For this line: For Each phonenr In oJSON.data("msg")

I am stuck working out how, for each event, I can loop through it and get the attributes from "msg" and the "_id" value as well.

回答1:

Wrap the str2 so that it is a valid JSON collection and update your code to iterate that collection as below.

<!--#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