【问题标题】:Looping through JSON using ASPJSON使用 ASPJSON 循环遍历 JSON
【发布时间】:2015-06-11 13:27:15
【问题描述】:

我正在使用 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”值中获取属性。

【问题讨论】:

  • 这个问题已经被问过了 - stackoverflow.com/q/30538292/692942。只是 OP 懒得接受答案,所以我不能将其标记为重复。
  • @Lankymart 感谢您的回复。查看您链接到的另一篇文章,您对如何获取的答案(在该示例中为 PitcherID)是询问 OP 是否有权控制 JSON 结构。就我而言,我无法这样做,因为我直接从 Mandrill 获取数据。既然如此,有什么方法可以访问 JSON 数据的两个块中的数据吗?谢谢
  • @Lankymart 对于副本,它需要有一个接受的答案或一个赞成的答案。因此,如果您认为另一个问题的答案是正确的,请点赞,然后标记为重复。
  • @Dijkgraaf 现在可以了,所以我已经标记了它,我不能很好地支持我自己的答案吗??
  • @StacPollaidh 同样适用,只需将来自 mandrill 的数据包装在包含对象 {data: [] }; 中。

标签: json asp-classic


【解决方案1】:

包装 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-13
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    相关资源
    最近更新 更多