2017-04-17 73 views
0

我正在使用ASPJson處理ASP和JSON(它曾經在此前託管http://www.aspjson.com/但該網站已不存在,但我從該網站獲得的代碼位於:https://pastebin.com/LJzikNAT循環瀏覽Youtube搜索JSON響應

這是我的電話到Youtube - 例如:

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

返回該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 
%> 

麻煩的是,因爲我遍歷它的ID在var_id變量返回總是5DniDm9epIY,這是第一視頻的ID - 它似乎並沒有被改變每次通過循環,我不知道爲什麼?

var_id顯示5次,因此代碼可以看到「items」集合中有5個註釋,但它並不是每次都通過循環轉到下一個節點。

+0

[使用VBScript訪問JSON數據的所有值]的可能的複製(http://stackoverflow.com/questions/41610139/using-vbscript-to-access-all-values-in-json -data) – Lankymart

回答

0

For Each永遠不會引用參考result對象,它將包含當前的枚舉值。

設置thisoJSON.data("items").item(thingy)將僅引用oJSON.data("items")集合中的第一個枚舉值。不知道什麼thingy是,但如果你在這種情況下,複製從其他地方這個代碼,應該是你與枚舉對象,result應使用(好像你從here得到它,然後在For Each但沒有改變thingyresult在代碼中使用它)

把上面一行

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

oJSON.data("items")集合中的引用返回到當前枚舉對象。


意識到我以前寫過關於此的內容,但自從我使用這個庫以來已經有一段時間了。

A: Using VBscript to access all values in JSON data

+1

謝謝你的幫忙@Lankymart。問題解決了。 – 4532066