Node.js怎么读写json文件?方法介绍
4252
nodejs怎么读写json文件?下面本篇文章给大家介绍一下nodejs读写json文件的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
读json文件
- 'use strict';
- const fs = require('fs');
- let rawdata = fs.readFileSync('student.json');
- let student = JSON.parse(rawdata);
- console.log(student);
写json文件:
- 'use strict';
- const fs = require('fs');
- let student = {
- name: 'Mike',
- age: 23,
- gender: 'Male',
- department: 'English',
- car: 'Honda'
- };
- let data = JSON.stringify(student);
- fs.writeFileSync('student-2.json', data);
虽然这是我们想要写入的数据,但数据是一行字符串的形式,这对我们来说很难读取。
如果您希望序列化的JSON是人类可读的,那么更改JSON。Stringify函数:
- let data = JSON.stringify(student, null, 2);
json 转为 csv
- // require json-2-csv module
- const converter = require('json-2-csv');
- const fs = require('fs');
- // read JSON from a file
- const todos = JSON.parse(fs.readFileSync('todos.json'));
- // convert JSON array to CSV string
- (async () => {
- try {
- const csv = await converter.json2csvAsync(todos);
- // print CSV string
- console.log(csv);
- // write CSV to a file
- fs.writeFileSync('todos.csv', csv);
- } catch (err) {
- console.log(err);
- }
- })();
csv转为json
- csv第一行为key,例如: id,name,email,country,age
- // require csvtojson module
- const CSVToJSON = require('csvtojson');
- // convert users.csv file to JSON array
- (async () => {
- try {
- const users = await CSVToJSON().fromFile('users.csv');
- // log the JSON array
- console.log(users);
- } catch (err) {
- console.log(err);
- }
- })();
本文网址:https://www.zztuku.com/index.php/detail-8620.html
站长图库 - Node.js怎么读写json文件?方法介绍
申明:如有侵犯,请 联系我们 删除。
您还没有登录,请 登录 后发表评论!
提示:请勿发布广告垃圾评论,否则封号处理!!