2016-05-04 51 views
1

私はこれまでhttp://www.aspjson.com/Looping through JSON Array using ASPJSONaspjsonとclassic aspを使ってjson配列を反復処理するには?

<!--#include file="aspjson.asp" --> 

jsonstring = "{""events"":""[{ 
""email"":""[email protected]"", 
""timestamp"":1461015166, 
""smtp-id"":""\[email protected]\u003e"", 
""event"":""processed"", 
""category"":""cat facts"", 
""sg_event_id"":""Y4SkYnMgWCH-j0Ap2HUBlg=="", 
""sg_message_id"":""14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"" 
}, 
{ 
"email"":""[email protected]"", 
""timestamp"":1461015166, 
""smtp-id"":""\[email protected]\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"":""[email protected]"", 
""timestamp"":1461015166, 
""smtp-id"":""\[email protected]\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 

からの助けを借りて、私が見つけた例のほとんどは、キーの名前を持っているが、私はいつも彼らが何であるかを知らない、これを来ています。

カテゴリに2つの要素が含まれている最後のセットにループが到着すると、問題が発生します。 "猫の事実"と "魚の事実"。

エラーメッセージは次のとおりです。 マイクロソフトの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 

私はまだ、このすべてが一緒にどのように適合するかに苦しんだことをその明らか確信している、誰もがあるかもしれない記述へのポインタまたはリンクを持っている場合ので、役に立つと大変感謝しています。

答えて

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 
+0

はそれをやった、ありがとうございました。 – slamslam102

関連する問題