Looping through JSON Array using ASPJSON

2020-05-08 14:12发布

问题:

Thanks to advice from here (Looping through JSON using ASPJSON) and here (ASP JSON: Object not a collection), I am starting with a JSON array in Classic ASP:

{"markers": [{
  "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
}]
}

I am using Classic ASP and ASPJSON (http://www.aspjson.com/).

I can access the "_id" and "ts" values from the loop via:

Set oJSON = New aspJSON

'read the local JSON data
oJSON.loadJSON(str2)

For Each thingy In oJSON.data("markers")

    Set this = oJSON.data("markers").item(thingy)
    Response.Write _
    this.item("_id") & ": " & _
    this.item("ts") & "<br>"

Next

However, I am stuck trying to get at the data one level down, in the "msg" section.

I did try:

For Each thingy In oJSON.data("markers")

    Set this = oJSON.data("markers").item(thingy)
    Response.Write _
    this.item("_id") & ": " & _
    this.item("ts") & "<br>"

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

Next

But get this error:

Description: Object not a collection : .

Relating to this line:

For Each thingy2 In oJSON.data("msg")

I guess I am doing something silly, but I am stuck on this one and can't work out how to access that data.

回答1:

You could access msg, which is a json object, within the loop you already have:

For Each thingy In oJSON.data("markers")

    Set this = oJSON.data("markers").item(thingy)
    Response.Write _
    this.item("_id") & ": " & _
    this.item("ts") & "<br>" &_
    this.item("msg").item("subject") & ": "  &_
    this.item("msg").item("diag") & "<br>"

Next