从以下元素中写入JSON文件夹

问题描述 投票:0回答:1

我想知道如何从以下地方读到写到 json 文件。

const Discord = require ('discord.js');
const client = new Discord.Client();
const {prefix, token,} = require('./config.json');
const fs = require ('fs');

    client.login(token)

        client.on('message', message => {
        if(message.content.startsWith(prefix + "TC")) { //TC = team create

                var args = message.content.split(' ').join(' ').slice(4);
                if(!args) return message.channel.send("No")

                var TeamCreate = `{"NameTeam": "${args}", "ManagerTeam": ${message.author.id}}`

            fs.writeFile("./team.json", TeamCreate, (x) => {
                if (x) console.error(x)
              })}});

json 文件将显示.NET文件,而我希望每次我们下订单时,它都会被添加到.NET文件中。{"NameTeam": "teste","ManagerTeam": 481117441955463169}

我希望每次我们下订单时,它都会被添加到 json 文件。

例如:我希望能从json文件夹中编译出以下项目。

1 first order = {"NameTeam": "teste","ManagerTeam": 481117441955463169}
2 second order = {"NameTeam": "teste","ManagerTeam": 481117441955463169}, {"NameTeam": "teste2","ManagerTeam": 1234567890}
javascript json discord.js fs
1个回答
0
投票

根据我的理解,你想做个 json 文件,其中包含一个团队的列表。

简单的方法是读取并解析json,对其进行修改,然后将json字符串化并更新文件。另外,用字符串来制作json真的很乱,可能会导致语法错误,而在javascript中,将js对象变成json就像做以下操作一样简单 JSON.stringify(javascriptObject)

试试这样的东西。

const Discord = require('discord.js');
const client = new Discord.Client();
const { prefix, token, } = require('./config.json');
const fs = require('fs');

client.login(token)

client.on('message', message => {
    if (message.content.startsWith(prefix + "TC")) { //TC = team create

        var args = message.content.split(' ').join(' ').slice(4);
        if (!args) return message.channel.send("No")

        var team = {
            NameTeam: args,
            ManagerTeam: message.author.id
        }

        fs.readFile("./team.json", (err, data) => {
            if (!err && data) {
                var parsedJson;
                try {
                    parsedJson = JSON.parse(data);
                    //Make sure the parsedJson is an array
                    if (!(parsedJson instanceof Array)) {
                        parsedJson = [];
                    }
                }
                catch (e) {
                    console.log("Couldn't parse json.");
                    parsedJson = [];
                }
                finally {
                    //Add the newly created team to parsedJson
                    parsedJson.push(team);

                    //Write file, stringifying the json.
                    fs.writeFile("./team.json", JSON.stringify(parsedJson), (err) => {
                        if (!err) {
                            console.log("Successfully created team.");
                        }
                        else {
                            console.log("Error writing to file.")
                        }
                    });
                }
            }
            else {
                console.log("Error reading json");
            }
        });
    }
});

希望能帮到你,祝你好运


0
投票

但我也希望能够修改JSON来为一个已经存在的团队添加元素。`1 first order = {"NameTeam": "teste", "ManagerTeam": 481117441955463169} 2秒订单={"NameTeam": "teste", "ManagerTeam": 481117441955463169: 481117441955463169, "member1": "@id", "member2": "@id"}。

© www.soinside.com 2019 - 2024. All rights reserved.