简体   繁体   中英

Difficulties in saving a json file

I have a function which connects to api and extracts particle , name and community_price for many various items. It works well but now I want it to save the data in a json file. It should look like this:

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

I spent plenty of time playing with fs.writeFileSync but unfortunately none of my attempts didn't work due to my poor knowledge of js. Any help is welcomed, the best answer will be selected. Thanks in advance!

Here's the function:

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()    
      });
}

This is what I suppose it should be, but still doesn't work.

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()    
  });
}

Following the official documentation tell that writeFileSync can be used as such:

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

I can't see any fs.writeFileSync in your code, but I guess this is what you want to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM