Looping through JSON using ASPJSON

陌路散爱 提交于 2019-12-10 11:57:13

问题


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


来源:https://stackoverflow.com/questions/30782144/looping-through-json-using-aspjson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!