Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript读取和编辑JSON文件_Javascript_Arrays_Json - Fatal编程技术网

使用Javascript读取和编辑JSON文件

使用Javascript读取和编辑JSON文件,javascript,arrays,json,Javascript,Arrays,Json,所以基本上我有一个机器人,如果你写信给它!帐户,它将根据您向其写入的ID向您提供信息。如果你有662的身份证并且正在写信给它!帐户,它将检查json文件中的steamID为662。如果有,它将显示您的货币和ID 所有这些我都明白了。我需要的帮助是,如果我的json中不存在您的ID。如果没有,我希望它被创建为一个新的 这是我的json代码: { "tradesettings": { "pollInterval": 5000, "confirmationChecker": 1

所以基本上我有一个机器人,如果你写信给它!帐户,它将根据您向其写入的ID向您提供信息。如果你有662的身份证并且正在写信给它!帐户,它将检查json文件中的steamID为662。如果有,它将显示您的货币和ID

所有这些我都明白了。我需要的帮助是,如果我的json中不存在您的ID。如果没有,我希望它被创建为一个新的

这是我的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 }
]}
当我想的时候,我想把前男友加入到整个事情中

        { "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;

            }
        }
    }
if(确定==1){
console.log(“[SERVER]”+streamid.getstreamid64()+“正在询问他的帐户信息。”);
对于(r=0;r
结果是:

[{ "botID":"3", "steamID":"662", "balance": 0 },
{ "botID":"2", "steamID":"211", "balance": 0 }]
如果您想使用JSON作为映射,那么

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 by 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 '';
}
函数findBySpecField(数据、请求字段、值、响应字段){
var容器=数据;
对于(变量i=0;i
在节点中,您应该使用与文件系统进行交互。在您的情况下,应该是这样的:

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'});
有些事情你需要注意:

  • 您的文件需要正确格式化,否则程序将崩溃
  • 如果你的应用程序写入了错误数据,那么你的配置文件是toast,你应该对其进行备份
  • 始终使用指定的显式编码进行读写

  • 您好,我只是想知道。您知道我想让它永远编辑我的config.json文件,而不仅仅是在我关闭bot之前。这不是那样工作的。所以您的问题其实不是如何更改类似json的对象,而是如何读取和写入文件中的javascript。有很多方法可以做到这一点。您需要更加具体c关于您正在运行的环境以及当前需要读取文件的内容。是否有一种方法可以私下给您写信,我有很多代码要向您展示。不,最好将其公开。如果您的代码是专有的,那么您应该能够提供一个简化版本来表达您的问题,但不存在任何问题任何私人信息。好的,我已经对我的问题进行了jsut编辑,以显示我的一些代码。您如何读取json文件?您使用的是节点还是浏览器?另外,请更新您的问题。您的问题不是关于数组和添加值,而是关于读取和写入json文件。出于某种原因,我得到了以下错误:return binding.writeS字符串(fd,buffer,offset,length,position);^Note;我已经连接到config.json,我已经从那里获得了一些信息,比如请求ID是有效的,只是传输它们是问题。这看起来不像是错误。消息的全文是什么?你是什么意思“已连接到json文件”?您的意思是可以读入它吗?传输是什么意思?您的意思是将其写入磁盘吗?我已经可以编写类似config.accounts[0].streamid的内容,它将为我提供第一个文件的streamid。我已经可以这样做了。我需要的是能够创建config.accounts[1].steamID,如果你知道我的意思。如果我们可以通过电子邮件讨论这个问题会容易得多。或者我可以在其他地方向你发送整个代码。不,我不知道你的意思。你不清楚。上面,你说你知道如何对对象进行更改,你只需要知道如何将其写入磁盘。但在这里,你是说有些不同。这个问题的其他答案描述了如何使用
    push
    将元素添加到数组中。
    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'});