3

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.

Claire_Monkey
  • 109
  • 1
  • 2
  • 13
  • 1
    You just have to keep building up the code using the pattern `Set = .item("array object").item("data")` whenever you want to pull the `data` collection. At the moment the code example points at `this.item("attachments")` which is just an object with a property called `data` that contains the collection. The `.items(i)` is only ever going to get the `data` collection so the `For Each` is pointless at this level which is why you need to be referencing `data` like *(in the example above)* `Set this2 = this.item("attachments").item("data").item("subattachments").item("data").item(i)`. – user692942 Nov 19 '15 at 14:07
  • I'm still not following. I have tried to set so many things to this2 and I mostly get: Object required: '[undefined]'. I get that the loop is pointless as there is only one item in it, but it's the only thing I could set to loop that didn't tell me it wasn't a collection. – Claire_Monkey Nov 19 '15 at 15:49
  • Got it! Shall answer now – Claire_Monkey Nov 19 '15 at 16:00

1 Answers1

3

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.

Claire_Monkey
  • 109
  • 1
  • 2
  • 13