A Simple Method for reading and writing json files by nodejs (mandatory), nodejsjson

Source: Internet
Author: User

A Simple Method for reading and writing json files by nodejs (mandatory), nodejsjson

Nodejs reads json files

Var fs = reauire ('fs'); var file = "d: \ 0. json "; var result = JSON. parse (fs. readFileSync (file); // operation object

Nodejs writes a json File

Var coors ={}; coors. left_top ={}; coors. right_top ={}; coors. left_bottom ={}; coors. right_bottom ={}; // fill the content var filename = "d :\\ coors \ 0 in coors. json "; fs. writeFileSync (filename, JSON. stringify (coors); // object nested var fs = require ('fs'); var fileDirectory = "D: \ column"; var newfileDirectory = "d: \ new "; if (fs. existsSync (fileDirectory) {var files = fs. readdirSync (fileDirectory); for (var I = 0; I < Files. length; I ++) {var filePath = fileDirectory + "/" + files [I]; var newfilepath = newfileDirectory + "/" + files [I] var filestr = JSON. parse (fs. readFileSync (filePath); for (var item in filestr) {var inter = filestr [item]; for (var m in inter) {inter [m] = parseFloat (inter [m]);} fs. writeFileSync (newfilepath, JSON. stringify (filestr) ;}} else {console. log (fileDirectory + "Not Found! ");}

The above Simple Method for reading and writing json files by nodejs (mandatory) is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for our guests.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.