How to edit an external JSON-file in JavaScript?

前端 未结 3 1524
执笔经年
执笔经年 2021-02-02 00:15

I have created a little chat bot following the tutorial of Esther Crawford. This bot check the string that enter the user and respond with one of my json answer.

For e

3条回答
  •  别那么骄傲
    2021-02-02 00:57

    You can't save in a file using client-side script, you have to use some server-side scripting like PHP, NodeJS etc. to save something in a file.

    For example in NodeJS you can use the fs library:

    fs = require('fs');
    var m = JSON.parse(fs.readFileSync('fileName.json').toString());
    m.forEach(function(p){
        p.name= m.name;
    });
    fs.writeFile('fileName.json', JSON.stringify(m));
    

    Hope it helps

提交回复
热议问题