如何在 JavaScript 中编辑外部 JSON 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36869615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:39:16  来源:igfitidea点击:

How to edit an external JSON-file in JavaScript?

javascriptjson

提问by Shark

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.

我按照Esther Crawford 的教程创建了一个小聊天机器人。这个机器人检查输入用户的字符串并用我的 json 答案之一进行响应。

For example if I say "hello"the bot 'll respond "Hey, I'm so glad you set EstherBot up!"

例如,如果我说“你好”,机器人会回应“嘿,我很高兴你设置了 EstherBot!”

script.json

脚本文件

{
    "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."
}

My question is: How to edit my script.jsonin JavaScript?

我的问题是:如何在 JavaScript 中编辑我的script.json

For the moment when the user enters an unknown string, the bot will answer that it doesn't understand.

当用户输入未知字符串时,机器人会回答它不理解。

script.js

脚本.js

if (!_.has(scriptRules, upperText)) {
    return bot.say('Sorry I dont understand').then(() => 'speak');
}

How could I get this unknown string of the user and add it in my script.jsonfile by editing in JavaScript my JSON-file ?

如何通过在 JavaScript 中编辑我的 JSON-file来获取用户的这个未知字符串并将其添加到我的script.json文件中?

I want that my bot learn by itself, if he doesn't know the answer it should automatically add the question of the user to the script.jsonfile, ask the user for an answer and then add this answer in the script.jsonfile too.

我希望我的机器人自己学习,如果他不知道答案,它应该自动将用户的问题添加到script.json文件中,询问用户答案,然后将此答案添加到script.json文件中也。

Many thanks for your help! You'll find this project on git with the full code here.

非常感谢您的帮助!你会在 git 上找到这个项目,这里有完整的代码。

回答by Vaibhav Jain

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.

您不能使用客户端脚本保存在文件中,您必须使用一些服务器端脚本(如 PHP、NodeJS 等)将某些内容保存在文件中。

For example in NodeJS you can use the fs library:

例如在 NodeJS 中,您可以使用 fs 库:

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

希望能帮助到你

回答by Jacob

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.

不幸的是,如果没有服务器端代码——这将需要一个请求并将文件存储在服务器上——就不可能保存文件。
但是,您可以使用 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.

你可以阅读更多 http://www.w3schools.com/html/html5_webstorage.asp
如果您希望它是临时的,您也可以使用会话存储。

回答by fapm84

Assuming you got your json already loaded:

假设您已经加载了 json:

var json = '{"hola":"ciao"}';

//Parse the JSON: convert it into an object
var parsedJson =JSON.parse(json);

//add whatever you want
parsedJson.hi = 'bye';

Your json var will look like:

您的 json var 将如下所示:

Object {hola: "ciao", hi: "bye"}

Object {hola: "ciao", hi: "bye"}

Then you can convert the object to string doing JSON.stringify(parsedJson)and write back to disk/DB if you're manipulating it in your backend (ie.: NodeJs).

然后,JSON.stringify(parsedJson)如果您在后端(即:NodeJs)中操作它,您可以将对象转换为字符串并写回磁盘/数据库。