所以基本上我有一个机器人,如果你写它!帐户,它会根据你写的ID给你一个信息。如果您的ID为662并写入它!帐户,它将检查json文件的steamID为662.如果有,它将显示您的货币和您的ID。
我已经想到了所有这一切。我需要帮助的是,如果你的身份证在我的json中不存在。如果它不是我想要它被创建为一个新的。
这是我的json代码:
{
"tradesettings": {
"pollInterval": 5000,
"confirmationChecker": 10000,
"acceptEscrow": false,
"acceptScammers": false,
"ourmaxitems": 50,
"theirmaxitems": 5
},
"accsettings": {
"amountofaccounts": 3
},
"accounts":[
{ "botID":"3", "steamID":"662", "balance": 0 },
{ "botID":"2", "steamID":"211", "balance": 0 },
{ "botID":"1", "steamID":"76561198026027024x", "balance": 666 },
{ "botID":"0", "steamID":"", "balance": 0 }
]}
当我想要时,我希望能够将ex添加到整个事物中
{ "botID":"4", "steamID":"421", "balance": 0 },
所以它说:
"accounts":[
{ "botID":"4", "steamID":"421", "balance": 0 },
{ "botID":"3", "steamID":"662", "balance": 0 },
{ "botID":"2", "steamID":"211", "balance": 0 },
{ "botID":"1", "steamID":"951", "balance": 666 },
{ "botID":"0", "steamID":"", "balance": 0 }]
以便使用新ID创建新帐户。我怎么做?? 哦,顺便说一句,我的机器人在myBot.js上,而这个json文件叫做config.json,同一个文件夹。
这是应该编辑它的代码。
if(ok == 1) {
console.log("[SERVER] "+steamID.getSteamID64()+" is asking for his account information.");
for(r=0;r<= config.accsettings.amountofaccounts ;r++) {
if (config.accounts[r].steamID != "undefined") {
if(config.accounts[r].steamID == steamID.getSteamID64()) {
console.log("[SERVER] "+ steamID.getSteamID64() +" is asking for his account info, respondig with it.\n")
client.chatMessage(steamID.getSteamID64(), "\nDisplaying your account on this bot\nBotID: "+config.accounts[r].botID+"\nSteamID: "+config.accounts[r].steamID+"\nBalance: "+config.accounts[r].balance);
break;
ok = 0;
r = 0;
}
}
if(r == config.accsettings.amountofaccounts) {
console.log("[SERVER] "+steamID.getSteamID64()+" dosen't have an account here, creating one for him.");
client.chatMessage(steamID.getSteamID64(),"Hold on, creating an account.\n\nDone, please type '!account' to view it\n(took 0.03 seconds)");
//var item2={ "botID":config.accsettings.amountofaccounts+1, "steamID":steamID.getSteamID64(), "balance": 0 };
//config.accounts.push(item2)
config.accounts.push({botId: "10", steamID: "11", balance: 0});
break;
r = 0;
ok = 0;
}
}
}
然后还有另一个文件是json文件,名为config.json
答案 0 :(得分:0)
var account=[];
var item={ "botID":"3", "steamID":"662", "balance": 0 };
var item2={ "botID":"2", "steamID":"211", "balance": 0 };
account.push(item);
account.push(item2);
结果是:
[{ "botID":"3", "steamID":"662", "balance": 0 },
{ "botID":"2", "steamID":"211", "balance": 0 }]
如果您想将JSON用作MAP,那么
var map = {};
// add a item
map[key1] = value1;
// or remove it
delete map[key1];
// or determine whether a key exists
key1 in map;
如果你想在JSON(数组)中创建新项目,这是JSON中不存在的ID字段...首先,你必须迭代到现有的JSON并根据函数的响应行事。
按需要字段查找项目(在我们的示例中为ID),您可以编写此功能。
function findBySpecField(data, requestedField, value, responseField) {
var container = data;
for (var i = 0; i < container.length; i++) {
if (container[i][requestedField] == value) {
return(container[i][responseField]);
}
}
return '';
}
答案 1 :(得分:0)
在节点中,您应该使用fs
module与文件系统进行交互。在你的情况下,它会是这样的:
const filename = `${__dirname}/config.json`;
const fs = require('fs');
let config = JSON.parse(fs.readFileSync(filename, {encoding: 'utf8'}));
// do your stuff
fs.writeSync(filename, JSON.stringify(config), {encoding: 'utf8'});
有些事情需要注意: