javascript - 将响应转换为 JSON/String 并写入文件
问题描述
我是java和node的新手,所以在尝试了2天之后......我写了这个问题。
我正在使用 git ( https://github.com/gigobyte/HLTV ) 并尝试使用从该 api 获得的响应来制作文件,但到目前为止我所得到的只是将结果写入控制台。
import HLTV from './index'
const fs = require('fs');
function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
sleep (1000)
//HLTV.getPlayerByName({ name: "chrisJ" }).then(res => this.Teste = res );
var Text = HLTV.getMatches().then(data => {console.log(JSON.stringify(data)); })
//var Texto = HLTV.getTeamRanking({ country: 'Brazil' });
//then(data => { console.log(JSON.stringify(data)); })
sleep(3000)
fs.writeFileSync('MyFile.json', Text)
console.log('Scoreboard update!')
有没有办法直接转换它并用字符串写一个文件?
解决方案
你必须在then
通话中这样做
HLTV.getMatches().then(data => {
var txt = JSON.stringify(data);
fs.writeFile('MyFile.json', txt, function (err) {
if (err) return console.log(err);
console.log('Data Saved');
});
});
推荐阅读
- spring - 具有类级别 url 注释的性能影响
- c# - 在屏幕共享程序中使用哈希存储图像缓冲区的内存管理
- laravel - 如何在 laravel 中批准多重身份验证
- regex - ora 06512/04088 INSERT INTO 语句时触发错误
- shell - 在 ubuntu 上安装 Opensmile
- javascript - 在 for 循环中使用 setInterval 递减
- javascript - moment isBetween 包括 from 和 to 日期
- c - C中的链表,如何修复预期的标识符错误?
- javascript - 如何在响应式幻灯片中添加播放按钮
- nopcommerce - nopcommerce API 将 Grant_type 代码转换为密码