在http://www.aspjson.com/的帮助下我已经走到了这一步。和 Looping through JSON Array using ASPJSON
<!--#include file="aspjson.asp" -->
jsonstring = "{""events"":""[{
""email"":""<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d373235331d29382e29733e3230" rel="noreferrer noopener nofollow">[email protected]</a>"",
""timestamp"":1461015166,
""smtp-id"":""\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e"",
""event"":""processed"",
""category"":""cat facts"",
""sg_event_id"":""Y4SkYnMgWCH-j0Ap2HUBlg=="",
""sg_message_id"":""14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0""
},
{
"email"":""<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="72170a131f021e1732061701065c111d1f" rel="noreferrer noopener nofollow">[email protected]</a>"",
""timestamp"":1461015166,
""smtp-id"":""\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e"",
""event"":""deferred"",
""category"":""cat facts"",
""sg_event_id"":""QVvoFUd9LS5083KTavaqAw=="",
""sg_message_id"":""14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"",
""response"":""400 try again later"",""attempt"":""5""
},
{
""email"":""<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a9acabad83b7a6b0b7eda0acae" rel="noreferrer noopener nofollow">[email protected]</a>"",
""timestamp"":1461015166,
""smtp-id"":""\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e"",
""event"":""processed"",
""category"":[""Cat Facts"",""Fish Facts""],
""sg_event_id"":""Y4SkYnMgWCH-j0Ap2HUBlg=="",
""sg_message_id"":""14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0""
}]""}"
Set jJSON = New aspJSON
jJSON.loadJSON(jsonstring)
For Each son In jJSON.data("events")
For Each grandson In jJSON.data("events").item(son)
response.write "<b>" & grandson & ": </b>" & jJSON.data("events").item(son).item(grandson) & "<br>"
Next
response.write "<br>"
Next
我发现的大多数示例都有键的名称,但我并不总是知道它们是什么。
当我的循环到达类别包含两个元素的最后一个集合时,我的问题就出现了。 “猫的事实”和“鱼的事实”。
错误信息是: Microsoft VBScript 运行时错误“800a01c2” 参数数量错误或属性分配无效
如果我能让这个工作正常,这将是朝着正确方向迈出的一步,但“类别”可以是另一条记录中的任何名称,并且可以有单个或多个值。
我想我需要添加一个 if/then 但我被困在这里。我想我想做这样的伪代码
if isarray(grandson) then
for each greatgrandson in grandson
response.write greatgrandson
else
response.write "<b>" & grandson & ": </b>" & jJSON.data("events").item(son).item(grandson) & "<br>"
end if
我确信我仍然在努力解决所有这些如何组合在一起的问题,因此如果有人有任何可能有帮助的指示或描述链接,我们将不胜感激。
最佳答案
您可以检查孙子是否是一个对象,以及它是否循环显示:
For Each son In jJSON.data("events")
For Each grandson In jJSON.data("events").item(son)
response.write "<b>" & grandson & ": </b>"
if NOT isObject(jJSON.data("events").item(son).item(grandson)) then
response.write jJSON.data("events").item(son).item(grandson)
else
for each category in jJSON.data("events").item(son).item(grandson)
Response.write jJSON.data("events").item(son).item(grandson)(category) & ","
next
end if
Response.write "<br>"
Next
response.write "<br>"
Next
关于arrays - 如何使用 aspjson 和经典 asp 迭代 json 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37016778/