-1
嘗試添加收到與POST請求轉換成JSON文件以這種格式的數據:寫入後接收到的數據JSON文件JSON格式
[ {
"id": 1,
"data": "hello",
"data1": "hi",
"data2": "hey",
} ]
但現在它這樣寫的:
id=1&data=hello&data1=hi&data2=hey
我讀過,我得先分析數據,這裏是代碼:
if (request.method == 'POST' && request.url == "/page") {
var body = "";
request.on('data', function (data) {
body += data;
});
request.on('end', function (req, res) {
POST = qs.parse(body);
console.log(POST);
var operation = POST.insert;
if (operation == 'insert') {
fs.readFile("file.json", "utf8", function (err, data){
var updateData = {
id: POST.id,
data: POST.data,
data1: POST.data1,
data2: POST.data2
}
var newData = qs.stringify(updateData);
fs.writeFile('file.json', newData, "utf8");
console.log(err);
})
}
});
}
如何將其轉化成JSON格式?
謝謝,幫助! –