繁体   English   中英

使用请求模块将json文件保存到计算机

[英]Saving a json file to the computer using The request module

我是编码方面的业余爱好者,因此请在这里寻求帮助。 我正在尝试使用Request模块从RescueTime请求数据。 -我已经弄清楚了如何获取数据主体,但是找不到将JSON文件写入/保存到本地硬盘的方法。 这是JS片段

var request = require("request");

request({ uri: "https://www.rescuetime.com/anapi/daily_summary_feed?key=MY_API_KEY", method: "GET" }, function(error, response, body) {
  var jsonfile = require('jsonfile')

  var file = '/Documents/request-playground/data.json'
  //Path on the local hard disk

  var obj = {name: 'JP'}

  jsonfile.writeFile(body, obj, {spaces: 2}, function(err) {
    console.error(err)
  })

});

看起来您刚刚从npm页面复制了示例。 如果要写入file ,则需要将第一个参数设为writeFile 在示例中,写入文件的有效负载为obj ,此处应为body

这应该为您解决问题:

var request = require("request");
var jsonfile = require('jsonfile');

request("https://www.rescuetime.com/anapi/daily_summary_feed?key=MY_API_KEY", function(error, response, body) {


  var file = '/Documents/request-playground/data.json'
  //Path on the local hard disk

  jsonfile.writeFile(file, body, {spaces: 2}, function(err) {
    console.error(err)
  })

});

暂无
暂无

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

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