【发布时间】:2016-12-25 22:50:33
【问题描述】:
我有这段代码,我需要它来从那个网站获取一些数据,即 apiurl 中的字符串。此代码需要下载这些网站,并在 json 中找到某些 appid。它需要下载这些网站上的数据并将它们存储在一个 json 文件中。由于某种原因,这不起作用。
我在 .js 文件中有这段代码:
var gamekeys = JSON.parse(fs.readFileSync('gamekeys.json'));
var jsonstring = JSON.stringify(gamekeys, null, 4);
UpdateGamePricelist();
function UpdateGamePricelist() {
for(var i = 0;i<gamekeys.keys.length;i++) {
appid = gamekeys.keys[i].appid;
var apiurl = "http://store.steampowered.com/api/appdetails?appids="+appid;
if (i < 95) {
request(apiurl, function(err, response, body) {
if (err) {
console.log("Error when updating game prices: " + err+"\n");
return;
}
var apiresponse = JSON.parse(body);
if (body == "") {
console.log("Could not find a pricelist m8");
return;
}
fs.writeFile("C:/Users/ahmad/Desktop/Bots/SteamKeyProfitter/gamespricelist/"+appid+".json", body, function(err) {
if(err) {
console.log("Error saving data to game pricelist: " + err);
return;
}
console.log("Game pricelist has been updated!");
});
});
}
}
}
我有一个json文件,这个json文件叫做gamekeys.json
这里是:
{
"keys": [
{
"appid": 10,
"price":0,
"listofkeys":[],
"game": "Counter-Strike"
},
{
"appid": 20,
"price":0,
"listofkeys":[],
"game": "Team Fortress Classic"
},
{
"appid": 30,
"price":0,
"listofkeys":[],
"game": "Day of Defeat"
},
{
"appid": 40,
"price":0,
"listofkeys":[],
"game": "Deathmatch Classic"
},
它当然会继续运行(200 万行)
为什么第一个代码没有创建95个json文件?
【问题讨论】:
标签: javascript arrays json node.js api