0

I am using ASPJson for working with ASP and JSON (it used to be hosted here a while ago http://www.aspjson.com/ but that site is no longer live, but the code I got from that site is here: https://pastebin.com/LJzikNAT)

This is my call to Youtube - example:

https://www.googleapis.com/youtube/v3/search?part=id&q=london&type=video&key=[my_key]

That returns this JSON data:

{
 "kind": "youtube#searchListResponse",
 "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/o9DTjpevDXudxmhkLef6i-kAnRE\"",
 "nextPageToken": "CAUQAA",
 "regionCode": "GB",
 "pageInfo": {
  "totalResults": 1000000,
  "resultsPerPage": 5
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/Qq093B1iIdU7htjV5jYf2Erqxgk\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "5DniDm9epIY"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/gbHSPn7IT-2OJG19vQZzKKTbG1s\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "Zlu542Tx8Fc"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/KQSQBNAk2ArZd_XrpDOIfiMT0XM\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "2tufxwCyrmE"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/wrbmiGkrH9v_QvtNpoIurXH9YQc\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "1XU8AOZ0Inw"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/2sdAsprKoDKIt8mNVYd8prR8uVA\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "vUO6kYLb6As"
   }
  }
 ]
}

This is my ASP code to try and loop through the results:

<!--#INCLUDE file="../dist/asp/c.asp" -->
<!--#INCLUDE file="../dist/asp/aspJSON.asp" -->
<%
my_api = "my_key"
sendstring1 = "https://www.googleapis.com/youtube/v3/search?part=id&q=chester&type=video&key="&my_api&""
Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
objXML.Open "GET", sendstring1 , false
objXML.Send()

BackFromGoogle1 = objXML.responseText

Set oJSON = New aspJSON
oJSON.loadJSON(BackFromGoogle1)

For Each result In oJSON.data("items")
    Set this = oJSON.data("items").item(thingy)
    var_id = this.item("id").item("videoId")
    embed_code = "<iframe width='800' height='450' src='https://www.youtube.com/embed/"&var_id&"?rel=0&amp;wmode=opaque' frameborder='0' allowfullscreen></iframe>"
    response.write embed_code   
Next
%>

The trouble is that as I loop through it, the ID returned in the var_id variable is always 5DniDm9epIY, which is the ID of the first video - it doesn't seem to be changing each time through the loop, and I'm not sure why?

The var_id is displayed 5 times, so the code can see that there are 5 notes in the "items" collection, but it doesn't appear to go to the next node each time through the loop.

user692942
  • 16,398
  • 7
  • 76
  • 175
4532066
  • 2,042
  • 5
  • 21
  • 48
  • Possible duplicate of [Using VBscript to access all values in JSON data](http://stackoverflow.com/questions/41610139/using-vbscript-to-access-all-values-in-json-data) – user692942 Apr 17 '17 at 09:24

1 Answers1

0

The For Each never references references the result object which will contain the current enumerated value.

Setting this to oJSON.data("items").item(thingy) will just reference the first enumerated value in the oJSON.data("items") collection. Not sure what thingy is but if you copied this code from somewhere else that should be the object you are enumerating with, in this case result should be used (seems like you got it from here then changed thingy to result in the For Each but not where it was used in the code).

Change that line to

Set this = oJSON.data("items").item(result)

to return a reference to the current enumerated object in the oJSON.data("items") collection.


Realised I've wrote about this before but been a while since I used this library.

A: Using VBscript to access all values in JSON data

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175