简体   繁体   English

从 json 文件获取数组到 javascript 数组

[英]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"
        ]
    }
}

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

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