I am using the Classic ASP JSON class from http://www.aspjson.com/ to convert a JSON feed from an external source for use within my site.
For a single level collection I'm doing fine.
JSON looks like this:
{
"data":[
{
"message":"message 5",
"id":"5"
},
{
"message":"message 4",
"id":"4"
},
{
"message":"message 3",
"id":"3"
},
{
"message":"message 2",
"id":"2"
},
{
"message":"message 1",
"id":"1"
}
]
}
and code works nicely like this:
TheFeed = [url of external json feed]
Set objXML=Server.CreateObject("Microsoft.XMLHTTP")
objXML.Open "GET", TheFeed, False
objXML.Send
strContents=objXML.ResponseText
Set objXML=Nothing
Set oJSON = New aspJSON
oJSON.loadJSON(strContents)
For Each i In oJSON.data("data")
Set this = oJSON.data("data").item(i)
response.write this.item("message")
end if
next
Now the feed however contains a lot more information, some of it in sub-collections of that top level collection, and I'm really struggling with working out how to access it, using the data/item options available to me. It may be I just can't do what I'm trying to do, which I'd really appreciate if someone could confirm that, but I am assuming it's more likely that I'm not getting my code right.
So my more complicated feed looks more like this (and as this is an external feed, I have no power over how that JSON is produced, I can only work with what I'm given):
{
"data":[
{
"message":"message 5",
"id":"5"
},
{
"message":"message 4",
"id":"4"
},
{
"message":"message 3",
"id":"3"
},
{
"message":"message 2",
"id":"2",
"attachments":{
"data":[
{
"subattachments":{
"data":[
{
"media":{
"image":{
"src":"1.jpg"
}
},
"type":"photo"
},
{
"media":{
"image":{
"src":"2.jpg"
}
},
"type":"photo"
},
{
"media":{
"image":{
"src":"3.jpg"
}
},
"type":"photo"
}
]
}
}
]
}
},
{
"message":"message 1",
"id":"1"
}
]
}
What I'd really love to do, is to have a secondary loop inside my initial loop to go through those photo attachments. But I'm just not sure how to get to the correct bit of the JSON to loop.
So far all I've managed to do that doesn't error at me is this:
If Not IsEmpty(this.item("attachments")) Then
for each i in this.item("attachments")
set this2 = this.item("attachments").item(i)
response.write 'here'
next
end if
That outputs a single 'here' only on message 2, which is what I'd expect, but I can't seem to do anything further. Everything I try using "subattachments" or anything else below the "attachment" level tells me "Object not a collection".
If anyone can shed any light on what I'm doing wrong, I would greatly appreciate it.