【问题标题】:How to remove specific sections in a json file如何删除 json 文件中的特定部分
【发布时间】:2021-06-25 02:07:32
【问题描述】:

好的,所以我有一个 JSON 文件,我在其中保留了一些用户 ID,以便返回某些命令。现在我想做的是能够使用命令添加和删除 id。我已经把所有的添加都弄清楚了,但是我一辈子都无法删除它们

else if (command === 'whitelist') { //command
var checker = whitelist; //whitelist json file
let test = Object.values(checker.users).find((value) => value.id === msg.author.id); //check id
if(!test)return msg.reply("Sorry you can't use this!");
if(test){
const user = msg.mentions.users.first();
if (!user) return msg.reply('Please mention a user!')
const json = require('./test.json') //teporary test file will be whitelist.json later
const item = Object.values(json.newdata).find(object => object.ID === user.id)
if (item){
msg.reply("Remove mentioned user from whitelist?")
msg.react(':thumbsup:').then(r => {
msg.react(':thumbsdown:');});
msg.awaitReactions((reaction, user) => user.id == msg.author.id && (reaction.emoji.name == ':thumbsup:' || reaction.emoji.name == ':thumbsdown:'),
{ max: 1, time: 30000 }).then(collected => {if (collected.first().emoji.name == ':thumbsup:') {

//here idk what to add to remove a sertain section from a json

msg.reply("Removed user!")}
else
msg.reply('Operation canceled.');
}).catch(() => {msg.reply('No reaction after 30 seconds, operation canceled');});
return
}
if (!item){ 
let rawdata = fs.readFileSync('./test.json');
msg.reply("Add mentioned user to whitelist?")
msg.react(':thumbsup:').then(r => {
msg.react(':thumbsdown:');});
msg.awaitReactions((reaction, user) => user.id == msg.author.id && (reaction.emoji.name == ':thumbsup:' || reaction.emoji.name == ':thumbsdown:'),
{ max: 1, time: 30000 }).then(collected => {if (collected.first().emoji.name == ':thumbsup:') {
let data = JSON.parse(rawdata);
data.newdata [user] = {"ID": user.id}
fs.writeFile("./test.json", JSON.stringify(data, null, 2), (err) => {
if (err) 
embed = new Discord.MessageEmbed()
.setTitle('Error report')
.addField(`Logged error:`, err, false)
.addField(`Server:`, `${msg.guild}`, false)
.addField(`Command:`, `${command} ${commandArgs}`, false)
.setColor('#0099ff');
webhookClient.send(embed)
})
msg.reply("Whitelisted user!")}
else
msg.reply('Operation canceled.');
}).catch(() => {msg.reply('No reaction after 30 seconds, operation canceled');});
return}}
}
在上面你可以找到整个命令 这是Json文件
{
  "newdata": {
    "<@804786710264938536>": {//for example if i want to i could remove this id from the file 
      "ID": "804786710264938536"
    },
    "<@361511417800556546>": {
      "ID": "361511417800556546"
    }
  }
}

【问题讨论】:

标签: javascript json discord.js


【解决方案1】:

JavaScript 有一个delete 操作符,所以你可以调用带有delete 操作符的JSON 来删除某个部分;

delete data.newdata
// OR
delete data['newdata']

More information on the delete operator

【讨论】:

  • 这似乎不起作用或者我只是做错了?你会这样做吗? let rawdata = fs.readFileSync('./whitelist.json'); let data = JSON.parse(rawdata); delete data.newdata msg.reply("Removed user!")}
  • @pitchedmobile 调用delete只会从加载到内存中的数据中删除,删除后需要写回json文件。
【解决方案2】:

好吧,我要做的就是这个

let rawdata = fs.readFileSync('./whitelist.json');
let data = JSON.parse(rawdata);
delete data.newdata [user]

【讨论】:

    猜你喜欢
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    相关资源
    最近更新 更多