title: Classic ASP JSON sub collection
body: <p>I am using the Classic ASP JSON class from <a href="http://www.aspjson.com/" rel="nofollow">http://www.aspjson.com/</a> to convert a JSON feed from an external source for use within my site. </p>
  
  <p>For a single level collection I'm doing fine. </p>
  
  <p>JSON looks like this:</p>
  
  <pre><code>{
     "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"
        }
     ]
  }
  </code></pre>
  
  <p>and code works nicely like this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p>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):</p>
  
  <pre><code>{
     "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"
        }
     ]
  }
  </code></pre>
  
  <p>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. </p>
  
  <p>So far all I've managed to do that doesn't error at me is this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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".</p>
  
  <p>If anyone can shed any light on what I'm doing wrong, I would greatly appreciate it. </p>