循环浏览 Youtube 搜索 JSON 响应

Looping through Youtube Search JSON response

我正在使用 ASPJson 与 ASP 和 JSON 一起工作(它曾经在这里托管过 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

这是我对 Youtube 的调用 - 示例:

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

那个returns这个JSON数据:

{
 "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"
   }
  }
 ]
}

这是我的 ASP 代码,用于尝试循环遍历结果:

<!--#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
%>

问题是当我遍历它时,var_id 变量中返回的 ID 始终是 5DniDm9epIY,这是第一个视频的 ID - 它似乎不是每次循环都在改变,我不确定为什么?

var_id显示了5次,所以代码可以看到"items"集合中有5条笔记,但是每次都不会出现跳转到下一个节点的情况循环。

For Each 从不引用引用包含当前枚举值的 result 对象。

this 设置为 oJSON.data("items").item(thingy) 将仅引用 oJSON.data("items") 集合中的第一个枚举值。不确定 thingy 是什么,但是如果您从其他地方复制此代码应该是您要枚举的对象,在这种情况下应该使用 result (好像你明白了从 然后在 For Each 中将 thingy 更改为 result 但不是在代码中使用它的地方).

将该行更改为

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

到 return 对 oJSON.data("items") 集合中当前枚举对象的引用。


意识到我以前写过关于这个的文章,但是我已经有一段时间没有使用这个库了。