如何使用aspjson与经典的asp迭代json数组?

时间:2016-05-04 01:03:33

标签: arrays json asp-classic

我在http://www.aspjson.com/Looping through JSON Array using ASPJSON

的帮助下走到这一步
<!--#include file="aspjson.asp" -->

jsonstring = "{""events"":""[{
""email"":""john@test.com"",
""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"":""example@test.com"",
""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"":""john@test.com"",
""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

我发现的大部分例子都有密钥的名称,但我并不总是知道它们是什么。

我的问题出现在我的循环到达最后一个集合,其中类别包含两个元素。 &#34; Cat Facts&#34;和&#34;鱼类事实&#34;。

错误信息是: Microsoft VBScript运行时错误&#39; 800a01c2&#39; 参数数量错误或属性分配无效

如果我能够做到这一点,那将是朝着正确方向迈出的一步,但是&#34;类别&#34;可以是另一个记录中的任何名称,它可以有单个或多个值。

我想我需要添加一个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

我确信很明显,我仍然在努力解决所有这些问题,所以如果有任何指针或指向可能有用的说明链接,我们将非常感激。

1 个答案:

答案 0 :(得分:1)

您可以检查孙子是否是一个对象,如果它是通过它循环显示:

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