简体   繁体   English

NodeJS:从 JSON 中提取数据,修改 object 并将其发送回同一个 JSON 文件

[英]NodeJS: Pulling data from JSON,modifying and Sending object back to the same JSON file

New to JSON, NodeJs and so on. JSON、NodeJs 等的新手。

What I am hoping to do is receive the following JSON file data to a NodeJS(and Angular) project modify the received data, then send it back to the JSON file.我希望做的是将以下 JSON 文件数据接收到 NodeJS(和 Angular)项目修改接收到的数据,然后将其发送回 JSON 文件。

JSON file: JSON 文件:

{
    "groups":{
        "groupid": [1,2,3],
        "name": ["g1","g2","g3"]
    },
    "rooms": {
        "room1": {
            "roomid": 1,
            "pGroup": ["g1","g2","g3"]
        },
        "room2": {
            "roomid": 2,
            "pGroup": ["g3"]
        },
        "room3": {
            "roomid": 3,
            "pGroup": ["g1","g2"]
        }
    }
} 

I can receive and store the data by doing the following:我可以通过执行以下操作来接收和存储数据:


        // Load in groups from groups.json

        fs.readFile('./data/groups.json', 'utf8', function(err, data){
            if (err) throw err;
            groups = [];
            let gArray = [];
            gArray = JSON.parse(data);
            console.log(gArray.groups.name[1]);
            for(let i = 0; i < gArray.groups.name.length; i++){
                groups.push(gArray.groups.name[i]);

            }           
        });

What I am currently using to store the data:我目前用于存储数据的内容:

                groups.push(newgroup);
                //store room in JSON file
                let groupString = JSON.stringify(groups);
                fs.writeFile('data/groups.json', groupString, 'utf-8', function(err){
                    if (err) throw err;
                });

I understand that the above puts it into a string and stores the array, but I've tried searching for an alternative where I can just edit the parts of the JSON file that I need, but I can't find an answer anywhere.我知道上面将它放入一个字符串并存储数组,但我尝试寻找一种替代方法,我可以在其中编辑我需要的 JSON 文件的部分,但我无法在任何地方找到答案。

any help could be appreciated.任何帮助都将不胜感激。

Thanks谢谢

In order to edit the contents of a file you need to actually write to the file为了编辑文件的内容,您需要实际写入文件

You can always just edit the properties of the json and keep the JavaScript object in memory ok the nodeJS application and if it's a server just send The memory -stored object to the client directly, do it looks like the file was updated when it really wasn't, but then when the server closes so the changes that weren't written to disk wouldn't be saved You can always just edit the properties of the json and keep the JavaScript object in memory ok the nodeJS application and if it's a server just send The memory -stored object to the client directly, do it looks like the file was updated when it really wasn 't,但是当服务器关闭时,不会保存未写入磁盘的更改

If you want to save your progress, you have to write to the disk (or some kind of database etc)如果要保存进度,则必须写入磁盘(或某种数据库等)

EDIT编辑

but there is still a way to do what you want, it's actually he a lot of databases work, but for json it might be a bit more complex但仍有一种方法可以做你想做的事,实际上他有很多数据库工作,但对于 json 它可能有点复杂

Basically the is a function in node is that can read only specific bytes of a file at a time基本上,节点中的 function 一次只能读取文件的特定字节

https://stackoverflow.com/a/23720178/2016831 https://stackoverflow.com/a/23720178/2016831

But the real way is to use fs.open then fs.write(look it up) to read/write to specific byte indexes of the file但真正的方法是使用 fs.open 然后 fs.write(look it up) 读取/写入文件的特定字节索引

The problem is how do you know what indexes of the file to write to, which is a whole science of how databases work etc usually binary is easier because it's split into different chunks and each chunk has some header bytes that refer to the byte index of the chunks before and after it etc then you need to read one root chunk which tells you what Uther chunks to read read etc until u find the part you why to edit, then after you do so you need to update some of the other chunks, or sometimes not necessarily etc look up how to make a database from scratch问题是你怎么知道要写入文件的哪些索引,这是一门关于数据库如何工作等的完整科学,通常二进制更容易,因为它被分成不同的块,每个块都有一些 header 字节,这些字节指的是之前和之后的块等然后你需要读取一个根块,它告诉你要读取的Uther块读取等等,直到你找到你为什么要编辑的部分,然后在你这样做之后你需要更新一些其他块,或者有时不一定等查找如何从头开始制作数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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