简体   繁体   中英

JSON only reading first element of array

I am working on a discord bot using Typescript, and I am having a problem reading from the config.JSON file. This is the read vars function:

fs.readFile('config.json', 'utf8', function readFileCallback(err, data){
        if (err){
            console.log(err);
        } else {
        config = JSON.parse(data); 

        console.log(config);

        const store = config.configstore[0];
        roster = config.roster[0];
    }});

and this is the config.json file:

{
"configstore": [
{"prefix": "!!"},
{"wallTime": 5},
{"bufferTime": 15},
{"wall": "false"},
{"buffer": "false"},
{"lastWallTime": "-1"},
{"lastBufferTime": "-1"},
{"wallCheckRole": "Role"},
{"bubfferCheckRole": "Role"}
],
"roster": [
{"discID":"discordID","ign":"IGN"}
]
}

When I print out the raw 'config' variable it prints this:

{
  configstore: [
    { prefix: '!!' },
    { wallTime: 5 },
    { bufferTime: 15 },
    { wall: 'false' },
    { buffer: 'false' },
    { lastWallTime: '-1' },
    { lastBufferTime: '-1' },
    { wallCheckRole: 'Role' },
    { bubfferCheckRole: 'Role' }
  ],
  roster: [ { discID: 'DiscordID', ign: 'IGN' } ]
}

But when I print the store variable it prints this: { prefix: '!!' }

The roster is normal as well.

The roles and ids are strings, but I changed it since I don't want them leaked.

These are some examples of what you are probably trying to achieve:

let config1 = {
        "configstore": [
            {"prefix": "!!"},
            {"wallTime": 5},
            {"bufferTime": 15},
            {"wall": "false"},
            {"buffer": "false"},
            {"lastWallTime": "-1"},
            {"lastBufferTime": "-1"},
            {"wallCheckRole": "Role"},
            {"bubfferCheckRole": "Role"}
        ],
        "roster": [
            {"discID":"discordID","ign":"IGN"}
        ]
    }   

let config2 = {
        "configstore": [
            {
                "prefix": "!!",
                "wallTime": 5,
                "bufferTime": 15,
                "wall": "false",
                "buffer": "false",
                "lastWallTime": "-1",
                "lastBufferTime": "-1",
                "wallCheckRole": "Role",
                "bubfferCheckRole": "Role"
            }
        ],
        "roster": [
            {"discID":"discordID","ign":"IGN"}
        ]
    }

//prints {"prefix":"!!"}
console.log("configstore1:", JSON.stringify(config1.configstore[0]));
//prints  {"discID":"discordID","ign":"IGN"}
console.log("roster1:", JSON.stringify(config1.roster[0]));

//prints {"prefix":"!!","wallTime":5,"bufferTime":15,"wall":"false","buffer":"false","lastWallTime":"-1","lastBufferTime":"-1","wallCheckRole":"Role","bubfferCheckRole":"Role"}
console.log("configstore2:", JSON.stringify(config2.configstore[0]));
//prints {"discID":"discordID","ign":"IGN"}
console.log("roster2:", JSON.stringify(config2.roster[0]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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