创建执行文件

F:\nodejs\test.js

var fs = require('fs'); //文件模块
var path = require('path'); //系统路径模块

//创建data对象
var data = {
    code: 0,
    ok: true,
    list: []
};

//通过for循环追加数据
for (var i = 0; i < 10; i++) {
    var obj = {
        name: '张三' + i,
        age: i
    };
    data.list.push(obj);
}

//把data对象转换为json格式字符串
var content = JSON.stringify(data); 

//指定创建目录及文件名称,__dirname为执行当前js文件的目录
var file = path.join(__dirname, 'data/test.json'); 

//写入文件
fs.writeFile(file, content, function(err) {
    if (err) {
        return console.log(err);
    }
    console.log('文件创建成功,地址:' + file);
});


在cmd命令输入指令运行test.js文件

这里写图片描述


用记事本打开test.json文件可以看到创建成功的内容

这里写图片描述

GitHub 加速计划 / js / json
19
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:5 个月前 )
34665ae6 binary -> binary_t Signed-off-by: Robert Chisholm <robert.chisholm@sheffield.ac.uk> 6 天前
f3dc4684 Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.9 to 3.28.10. - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/9e8d0789d4a0fa9ceb6b1738f7e269594bdd67f0...b56ba49b26e50535fa1e7f7db0f4f7b4bf65d80d) --- updated-dependencies: - dependency-name: github/codeql-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 13 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐