我想從YouTube視頻的信息。從我看起來像這樣的一個文件,獲取ID:For循環不追加到其他JSON在原來的順序
songlist.json
["vbMQfaG6lo8", "07UwP3kHTTk", "gjDK4OiuIfk", "NlrK03iz74A", "E_1-oylPHjs"]
之後,我需要的文件,還需要數據的每個ID
var songlistshit = './static/js/songlistinfo.json'
var allSongs = require('./static/js/songlist.json')
for(var i = 0; i < allSongs.length; i++) {
var ytId = allSongs[i]
request("https://www.googleapis.com/youtube/v3/videos?id=" + ytId + "&key=API_KEY%20&part=snippet,contentDetails,statistics,status", function (error, response, body) {
var sd = JSON.parse(body);
var xd = sd.items[0].snippet.title
var aus = sd.items[0].snippet
name = aus.title
tn = aus.thumbnails.default.url
var data = '\n{"name": "' + name + '", "user": "Mstiekema", "img": "' + tn + '"},'
fs.appendFile(songlistshit, data, 'utf8', function(err) {if (err) {return}})
})
};
但當它附加到第二個JSON文件時,它會丟失原始順序。這是一個如何將它放入文件的例子。
songlistinfo.json
{"name": "Enjoy The Silence by KI Theory (Ghost In The Shell Trailer Music)", "user": "Mstiekema", "img": "https://i.ytimg.com/vi/E_1-oylPHjs/default.jpg"},
{"name": "Majora's Mask - Terrible Fate", "user": "Mstiekema", "img": "https://i.ytimg.com/vi/vbMQfaG6lo8/default.jpg"},
{"name": "Lilium Elfen Lied-エルフェンリート", "user": "Mstiekema", "img": "https://i.ytimg.com/vi/07UwP3kHTTk/default.jpg"},
{"name": "MODERN PILLOW TALKING", "user": "Mstiekema", "img": "https://i.ytimg.com/vi/gjDK4OiuIfk/default.jpg"},
{"name": "Midna's Theme on Guitar", "user": "Mstiekema", "img": "https://i.ytimg.com/vi/NlrK03iz74A/default.jpg"},
但它不應該是這個順序。順序應該是如下:
songlistinfo.json
{"name": "Majora's Mask - Terrible Fate", "user": "Mstiekema", "img": "https://i.ytimg.com/vi/vbMQfaG6lo8/default.jpg"},
{"name": "Lilium Elfen Lied-エルフェンリート", "user": "Mstiekema", "img": "https://i.ytimg.com/vi/07UwP3kHTTk/default.jpg"},
{"name": "MODERN PILLOW TALKING", "user": "Mstiekema", "img": "https://i.ytimg.com/vi/gjDK4OiuIfk/default.jpg"},
{"name": "Midna's Theme on Guitar", "user": "Mstiekema", "img": "https://i.ytimg.com/vi/NlrK03iz74A/default.jpg"},
{"name": "Enjoy The Silence by KI Theory (Ghost In The Shell Trailer Music)", "user": "Mstiekema", "img": "https://i.ytimg.com/vi/E_1-oylPHjs/default.jpg"},
(我知道songlistinfo.json文件不包含正確的JSON結構,但我會修復後固定的訂單問題。)
你可能要刪除其中包含在URL中你的API密鑰的部分。 – devilfart