简体   繁体   English

如何将json对象从POST请求写入/附加到JSON文件?

[英]How to write/append the json object from a POST request to a JSON file?

I want to add a JSON element to a JSON file using the data given by a POST request. 我想使用POST请求提供的数据将JSON元素添加到JSON文件中。 This already kind of works, I just can't figure out how to add the id to the data, how do I do that? 这已经很有效了,我只是弄清楚如何将id添加到数据中,我该怎么做?

I tried a lot, I have created a completely new JSON object and tried to add it to my file which didn't work and I tried adding data to the data given from the POST request like this: body += { "id": 10 }; 我尝试了很多,我创建了一个全新的JSON对象,并尝试将其添加到我的文件中,但是我没有工作,我尝试将数据添加到POST请求中给出的数据,如下所示:body + = {“id”: 10}; which produces an undefined error. 这会产生一个未定义的错误。

Here is how I handle the POST request: 以下是我处理POST请求的方法:

} else if (req.method === 'POST' && pathname === 'Kunden') {

        res.writeHead(200, {
            'Content-Type' : 'application/json'
        });

        var body = '';
        req.on('data', function(data) {
            body += data;
        });

        req.on('end', function() {

            fs.readFile('Kunden.json', function(err, kundenJSON) {
                if (err) {
                    console.log(err);
                }

                var kunden = JSON.parse([kundenJSON]);
                kunden.kunde.push(JSON.parse(body));
                kundenJSON = JSON.stringify(kunden);

                fs.writeFile('Kunden.json', kundenJSON, function(err) {
                    if(err) {console.log(err);
                    }});
            });

            });
    }

}).listen(8081);

and here is my already existing JSON file: 这是我已经存在的JSON文件:

{"kunde":[{"id":1,"name":"Customer1"},{"id":2,"name":"Customer2"},{"id":3,"name":"Customer3"}]}

Basically I get the "name" from the POST req and I have to add the following id (in the first request this would be 4, then 5 and so on) to it and then append it to my file. 基本上我从POST req获取“名称”,我必须添加以下id(在第一个请求中,这将是4,然后是5,依此类推),然后将其附加到我的文件中。

In the end my file should look like this: 最后我的文件应如下所示:

{"kunde":[{"id":1,"name":"Customer1"},{"id":2,"name":"Customer2"},{"id":3,"name":"Customer3"},{"id":4,"name":PostData"}]}

But I can only manage this right now: 但我现在只能管理这个:

{"kunde":[{"id":1,"name":"Customer1"},{"id":2,"name":"Customer2"},{"id":3,"name":"Customer3"},{"name":PostData"}]}

If I understand you correctly, you want to add and id to the new object when you save it to the file. 如果我理解正确,您希望在将新对象保存到文件时添加和id。 This is basically easy, but you should also know how to keep track of the ids properly. 这基本上很简单,但您也应该知道如何正确跟踪ID。

var kunden = JSON.parse([kundenJSON]);
var newKunde = JSON.parse(body);
newKunde.id = 4;
kunden.kunde.push(newKunde);
kundenJSON = JSON.stringify(kunden);

The problems I'm talking about is, how to you get to know the new id? 我正在谈论的问题是,如何了解新的身份证? You could just say newId = kunden.kunde.length + 1 - but if you delete one later on, you will have duplicate ids. 你可以说newId = kunden.kunde.length + 1 - 但如果你稍后删除一个,你将有重复的id。 You also need to think about how to handle unique ids for the future. 您还需要考虑如何处理未来的独特ID。

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

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