【问题标题】:Get array from a json file to a javascript array从 json 文件获取数组到 javascript 数组
【发布时间】:2020-09-09 22:11:28
【问题描述】:

预脚本:我是 javascript 的菜鸟,我正在使用我的 c# 知识在 javascript 中创建一个 Discord 机器人,所以如果这个问题太愚蠢,我很抱歉。

我有一个代码可以在一个 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 是有人在我的 Discord 服务器中发送w_add task_[subcomando] 时的消息。示例如下)

输出的json文件tarefas.json为:

{
"wait tasks": {
    "tasks": []
}

目前,当机器人重新启动时,我的代码会重置数组 tarefas 的值,因此我想从 tarefas.json 文件中重新加载它的值,确切地说是从 "tasks": [] 数组中。我该怎么做?

额外: json 文件填充一些“任务”时的示例:

screenshot of my discord

{
    "wait tasks": {
        "tasks": [
            "hi",
            "hello",
            "bye",
            "math exam tomorrow"
        ]
    }
}

【问题讨论】:

    标签: javascript arrays json file


    【解决方案1】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-11
      • 2014-02-22
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多