【问题标题】:Saving a json file to the computer using The request module使用请求模块将 json 文件保存到计算机
【发布时间】:2018-05-17 08:20:37
【问题描述】:

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

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

});

【问题讨论】:

  • 查看fs模块
  • Here's the JS snippet - 该死的难以阅读...使用代码块(编辑器菜单中的{}

标签: javascript json api https request


【解决方案1】:

看起来您只是从 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)
  })

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-06
    • 2018-02-08
    • 2016-11-09
    • 2016-04-02
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 2015-09-16
    相关资源
    最近更新 更多