使用ASPJSON循环通过JSON数组

时间:2015-06-12 12:03:39

标签: json asp-classic

感谢来自这里(Looping through JSON using ASPJSON)和这里(ASP JSON: Object not a collection)的建议,我开始使用经典ASP中的JSON数组:

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

我使用的是经典ASP和ASPJSON(http://www.aspjson.com/)。

我可以通过以下方式从循环中访问“_id”和“ts”值:

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

但是,在“msg”部分中,我一直试图将数据降低一级。

我确实尝试过:

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

但是得到这个错误:

  

描述:对象不是集合:。

关于这一行:

For each thingy2 in oJSON.data(“msg”)

我想我正在做一些愚蠢的事情,但我仍然坚持这个,并且无法弄清楚如何访问这些数据。

1 个答案:

答案 0 :(得分:0)

您可以在已有的循环中访问msg,这是一个json对象:

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