Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Classic ASP JSON sub collection

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.

like image 803
Claire_Monkey Avatar asked May 17 '26 20:05

Claire_Monkey


1 Answers

I was not using the 'data' item enough.

This code:

If Not IsEmpty(this.item("attachments")) Then
 for each i in this.item("attachments").item("data").item(0).item("subattachments").item("data")
  set this2 = this.item("attachments").item("data").item(0).item("subattachments").item("data").item(i)
  response.write "here"
 next
end if

Returns the multiple "here"s I was hoping for. I could also have done a loop within a loop, but as the first collection only had one item in it, I just referenced it using the 0 index.

I should now have no problem digging out the items I need from he subattachments.

like image 139
Claire_Monkey Avatar answered May 21 '26 01:05

Claire_Monkey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!