gpt4 book ai didi

javascript - 将 json 对象保存为文本文件

转载 作者:行者123 更新时间:2023-12-03 08:06:12 25 4
gpt4 key购买 nike

我正在使用名为 DeepBot 的 Twitch.tv 流媒体机器人的 API。

这是 github 上的链接 https://github.com/DeepBot-API/client-websocket

我的目标是创建一个文本文档,列出使用命令 api|get_users| 从机器人中提取的所有信息。机器人的响应始终是一个 json 对象。如何从机器人获取 json 对象并将其保存为文本文件?

编辑:我的代码

var WebSocket = require('ws');
var ws = new WebSocket('ws://Ip and Port/');
ws.on('open', function () {
console.log('sending API registration');
ws.send('api|register|SECRET');
});

ws.on('close', function close() {
console.log('disconnected');
});
ws.on('message', function (message) {
console.log('Received: ' + message);
});

ws.on('open', function () {
ws.send('api|get_users|');
});

最佳答案

这取决于您的设置如何?您在 javascript 下发布了此内容。所以我猜你是:

  • 使用浏览器建立 Websocket 连接,以防无法直接在客户端保存文件。但在 HTML5 中,您可以使用本地存储来存储键值对。
  • 使用 Node js(服务器端 JavaScript),代码如下:
  • 一些其他设置,我无法猜测。万一你可以多说一点吗?

在具有 HTML5 功能的浏览器中:

// where msg is an object returned from the API
localStorage.setItem('Some key', JSON.stringify(msg));

在 Node.js 中

var fs = require("fs"); // Has to be installed first with “npm install fs”

// where msg is an object returned from the API
fs.writeFile("some-file.json", JSON.stringify(msg), function (err) {
if (err) throw err;
});

编辑:好的,感谢您的清理。我相信 Blag 的解决方案是可行的方法。

祝您的项目顺利!

关于javascript - 将 json 对象保存为文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34375416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com