使用 ASPJSON 循环遍历 JSON [英] Looping through JSON using ASPJSON
问题描述
我正在使用 Classic ASP 和 ASPJSON (http://www.aspjson.com/) 来尝试循环遍历通过 Mandrill Webhook 返回的 JSON 测试数据.
I am using Classic ASP and ASPJSON (http://www.aspjson.com/) to try to loop through JSON test data returned via a Mandrill Webhook.
这是示例 JSON 数据 - 我意识到第二个块与第一个块相同,但我需要使用它,因为当我在实时系统上执行此操作时,webhook 数据将在单个 JSON 文件中分批返回/发帖.
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
}
在我的测试 ASP 页面中,我尝试了一个简单的测试(我已经通过将 JSON 数据保存到数据库并检查内容来确认它是有效的)
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
Mandrill 调用 ASP 页面时,返回此错误:
When Mandrill calls the ASP page, it returns this error:
描述:对象不是集合:.
Description: Object not a collection : .
对于这一行:For Each phonenr In oJSON.data("msg")
For this line: For Each phonenr In oJSON.data("msg")
我一直在研究如何对每个事件进行循环,并从msg"和_id"值中获取属性.
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.
推荐答案
包装 str2 使其成为有效的 JSON 集合并更新您的代码以迭代该集合,如下所示.
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
这篇关于使用 ASPJSON 循环遍历 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!