Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中将json对象写入.json文件_Javascript_Json - Fatal编程技术网

在JavaScript中将json对象写入.json文件

在JavaScript中将json对象写入.json文件,javascript,json,Javascript,Json,如何在javascript的.JSON文件中以JSON格式写入项目数据?(在ES 5.1中介绍)将对象转换为JSON字符串 var proj = { Name : "abc", Roll no : 123 }; JavaScript没有写入文件的内置机制。您通常需要主机环境提供的一些非标准的东西。例如,Node.js具有文件系统模块的方法。您可以像这样将json对象保存在File.json文件中 var json = JSON.stringify(proj); JSON.st

如何在javascript的.JSON文件中以JSON格式写入项目数据?

(在ES 5.1中介绍)将对象转换为JSON字符串

var proj = {
    Name : "abc",
    Roll no : 123
};

JavaScript没有写入文件的内置机制。您通常需要主机环境提供的一些非标准的东西。例如,Node.js具有文件系统模块的方法。

您可以像这样将json对象保存在File.json文件中

var json = JSON.stringify(proj);

JSON.stringify(proj)
将其从Javascript转换为JSON。通过一门JSON基础课程来教自己如何从头开始创建JSON是值得的,但这是一个非常基本的问题。你有两个答案,完全相同且有效,解决了你的问题。应该是
(err)=>{if(err)throw err;}
const FileSystem = require("fs");
 FileSystem.writeFile('file.json', JSON.stringify(proj), (error) => {
    if (error) throw error;
  });