简体   繁体   English

如何将数据从控制台日志传输到 json 文件?

[英]How do I transfer data from console log to a json file?

There is a script that outputs data to console.有一个脚本可以将数据输出到控制台。 log, how can I put data from console.log in a separate json file?日志,如何将来自 console.log 的数据放在单独的 json 文件中?

var store = require('app-store-scraper');

 store.search({
    term: 'ninja',
    num: 2,
    page: 3,
    country : 'us',
    lang: 'lang'
    })
  .then(console.log)
  .catch(console.log);

You can use the fs module as:您可以将 fs 模块用作:

var fs = require('fs');
var jsonData = {}    // Your Json data 
fs.writeFile ("output.json", JSON.stringify(jsonData), function(err) {
    if (err) throw err;
    console.log('complete');
    }
);

The easiest way around is to use >> operator最简单的方法是使用>>运算符

node your_file.js >> file.json

Here's the solution:这是解决方案:

const fs = require('fs');
const store = require('app-store-scraper');

store.search({
    term: 'ninja',
    num: 2,
    page: 3,
    country : 'us',
    lang: 'lang'
})
.then(response => {
    fs.writeFileSync('file.json', JSON.stringify(response));
})
.catch(err => {
    fs.writeFileSync('file.json', JSON.stringify(err));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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