[英]Get array from a json file to a javascript array
pre-script: I'm a very noob at javascript and I'm using my c# knoledge to create a Discord bot in javascript, so I'm sorry if this question is too dumb. pre-script: I'm a very noob at javascript and I'm using my c# knoledge to create a Discord bot in javascript, so I'm sorry if this question is too dumb.
I have a code to write down in a json file an array called tarefas
using this code:我有一个代码要在 json 文件中写下一个名为tarefas
的数组,使用以下代码:
tarefas.push(subcomando);
bot.tarefasFile ['wait tasks'] = {
task : tarefas
}
fs.writeFile('./tarefas.json', JSON.stringify(bot.tarefasFile,null,4),
err => {
if (err) throw err;
});
cmd.channel.send('`' + subcomando + '` added to tasks.');
( subcomando
is the message when someone sends w_add task_[subcomando]
in my Discord server. Example below) ( subcomando
是有人在我的 Discord 服务器中发送w_add task_[subcomando]
时的消息。示例如下)
The output json file tarefas.json
is: output json 文件tarefas.json
是:
{
"wait tasks": {
"tasks": []
}
For now, my code resets the value of the array tarefas
when the bot restarts, so I want to reload its values from the tarefas.json file, precisely from the "tasks": []
array.现在,当机器人重新启动时,我的代码会重置数组tarefas
的值,所以我想从 tarefas.json 文件中重新加载它的值,正是来自"tasks": []
数组。 How do I do that?我怎么做?
extra: Example from when the json file is populated with some "tasks":额外:当 json 文件填充一些“任务”时的示例:
screenshot of my discord我的 discord 的屏幕截图
{
"wait tasks": {
"tasks": [
"hi",
"hello",
"bye",
"math exam tomorrow"
]
}
}
Fixes by using txt file instead of json file.通过使用 txt 文件而不是 json 文件进行修复。 write: https://www.geeksforgeeks.org/javascript-program-to-write-data-in-a-text-file/ read: Reading local text file into a JavaScript array fix the split function not working: What is causing the error `string.split is not a function`?写: https://www.geeksforgeeks.org/javascript-program-to-write-data-in-a-text-file/读: 将本地文本文件读入 JavaScript 数组修复拆分 ZC1C425245074AB7 导致的原因:A9384F111 错误“string.split 不是函数”?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.