当前第2页 返回上一页
或者,可以使用readfilesync函数读取JSON文件内容。创建一个包含以下内容的readjsonfilesync.js文件
文件名:readjsonfilesync.js
1 2 3 4 5 6 7 8 9 10 11 12 13 | var jsonFile = require( 'jsonfile' )
var fileName = 'employee.json'
var jsonData = jsonFile.readFileSync(fileName);
for (var i = 0; i < jsonData.length; ++i) {
console.log( "Emp ID : " +jsonData[i].emp_id);
console.log( "Emp Name : " +jsonData[i].emp_name);
console.log( "Emp Address : " +jsonData[i].emp_addr);
console.log( "Designation : " +jsonData[i].designation);
console.log( "----------------------------------" );
}
|
现在使用以下命令运行nodejs脚本。
1 2 3 4 5 6 7 8 9 10 11 12 | $ node ReadJsonFileSync.js
Emp ID: 101
Emp Name : Mike
Emp Address: 123 California, USA
Designation: Editor
Emp ID: 102
Emp Name : Jacob
Emp Address: 456 Log Angelis, USA
Designation: Chief Editor
|
本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的node.js视频教程栏目!!!
以上就是如何使用Node.js读取JSON文件的详细内容,更多文章请关注木庄网络博客!
返回前面的内容
相关阅读 >>
如何从javascript到typescript?
node.js 15正式版发布,将替代node.js 14成为当前的的稳定发行版
分享几种实用的node.js调试方法,快来收藏吧!!
了解nodejs中的事件和事件循环
浅谈node连接mysql数据库的方法
2021年值得了解的8个 node.js 框架
node.js是什么?有什么优势?有什么用途?
node.js中lts和current有什么区别
node.js中的全局对象有哪些?有什么用?
node.js是什么?优势是什么?
更多相关阅读请进入《node.js》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 如何使用Node.js读取JSON文件