How to edit an external JSON-file in JavaScript?

前端 未结 3 1523
执笔经年
执笔经年 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 01:08

    Unfortunately, without having server-side code - that would take a request & store the file on the server - it is not posible to save files.
    However you could use localStorage.

    For example:

    //If statement to check if localStorage already stored.
    if (!localStorage.script) {
    
        localStorage.script = JSON.stringify({
    "HELLO": "Hey, I'm so glad you set EstherBot up!",
    "I LOVE YOU": "Awh, shucks! I love you too!",
    "CONNECT ME": "",
    "DISCONNECT": "Roger that, EstherBot is back."
    }) ;
    
    }
    
    //This will load the localStorage data into an object in the varaible called botScript
    var botScript = JSON.parse(localStorage.script) ;
    
    function saveScript() {
    
        //This will save the current object to the localStorage.
        localStorage.script = JSON.stringify(botScript) ;
    
    }
    

    You can read more at http://www.w3schools.com/html/html5_webstorage.asp.
    You could also use session storage if you want it to be temporary.

提交回复
热议问题