【问题标题】:Difficulties in saving a json file保存json文件的困难
【发布时间】:2020-04-19 12:26:36
【问题描述】:

我有一个函数可以连接到 api 并为许多不同的项目提取 particlenamecommunity_price。它运行良好,但现在我希望它将数据保存在 json 文件中。它应该是这样的:

[
    {
        "particle": "xxx",
        "name": "xxx",
        "community_price": "xxx"
    },
    {
        "particle": "xxx",
        "name": "xxx",
        "community_price": "xxx"
    },
    {
        "particle": "xxx",
        "name": "xxx",
        "community_price": "xxx"
    }
]

我花了很多时间玩fs.writeFileSync,但不幸的是,由于我对 js 的了解不足,我的尝试都没有成功。欢迎任何帮助,将选择最佳答案。提前致谢!

函数如下:

function get_community_price(){

  let url = 'https://example.com/api/IGetPrices/v4?key=' + backpackkey;
  var massive;
  let quality = '5';
  request({url:url, json:true},  function(err, res, body){
        if (err) console.log(err);

        massive = body.response.items;
        console.log('recieved comminty prices')
        for(let i = 0; i < data.length; i += 1){

          let get_json = other.urltojson(data[i].ApiLink);
          let particle = get_json.particle
          let name = data[i].BuyLink;
              name = name.replace(/(\/Tradable).*/, '');
              name = name.replace(/.*(\/)/, '');
              name = name.replace(/\%20/g, ' ');
              name = name.replace(/\%27/g, '\'')

          let community_price = massive[name].prices[quality].Tradable.Craftable[particle].value;
          data[i].BuyPrice = community_price;

        }
         CreateOrders()    
      });
}

这是我认为应该的,但仍然不起作用。

function get_community_price(){

  let url = 'https://example.com/api/IGetPrices/v4?key=' + backpackkey;
  var massive;
  let quality = '5';
  request({url:url, json:true},  function(err, res, body){
        if (err) console.log(err);

        massive = body.response.items;
        console.log('recieved comminty prices')
        for(let i = 0; i < data.length; i += 1){

          let get_json = other.urltojson(data[i].ApiLink);
          let particle = get_json.particle
          let name = data[i].BuyLink;
              name = name.replace(/(\/Tradable).*/, '');
              name = name.replace(/.*(\/)/, '');
              name = name.replace(/\%20/g, ' ');
              name = name.replace(/\%27/g, '\'')

          let community_price = massive[name].prices[quality].Tradable.Craftable[particle].value;
          data[i].BuyPrice = community_price;

          var priceLog = [];

          priceLog.push({particle, name, community_price});
        }

  fs.writeFileSync('./app/comPrice.json', JSON.stringify(priceLog, null, 4));
  console.log('Fresh community price saved.');

  CreateOrders()    
  });
}

【问题讨论】:

  • 请展示您使用fs.writeFileSync 的尝试,以便我们解释您做错了什么。
  • 我编辑了我的问题并添加了我的尝试
  • var priceLog = [];移出for循环,您可以将其设置为const而不是var
  • @Gearge 是的,现在效果很好。感谢您的帮助,我非常感谢您

标签: javascript node.js json file fs


【解决方案1】:

official documentation 之后告诉 writeFileSync 可以这样使用:

const fs = require("fs");
const json = { /* Big object of whatever */ };
const path = "path/file.json";
fs.writeFileSync(path, JSON.stringify(json));

我在您的代码中看不到任何 fs.writeFileSync,但我想这就是您想要做的。

【讨论】:

  • 是的,现在我意识到在这里发帖时我需要展示我已经尝试过的内容。我编辑了我的问题,检查一下。
  • 你的代码应该可以工作。你有错误吗?
  • 在发布问题之前,我将priceLog.push({particle, name, community_price}); 写在 2 个车道上,结果抛出了let community_price = massive[name].prices[quality].Tradable.Craftable[particle].value; ^ TypeError: Cannot read property 'one of the names' of undefined
  • 现在它写在 1 个通道中,它可以工作但只保存 1 个项目而不是很多
  • var priceLog = []; 移出循环有帮助
猜你喜欢
  • 2020-06-23
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
相关资源
最近更新 更多