In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to solve nodejs json Chinese garbled code". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Nodejs json Chinese garbled solutions: 1, check whether the json file is UTF-8 without BOM coding; 2, save the file in UTF8 encoding format; 3, read with binary encoding, and then decode it with GBK.
This article operating environment: windows7 system, nodejs10.16.2 version, Dell G3 computer.
What about the Chinese garbled code in nodejs json?
The solution of garbled codes when nodejs reads local Chinese json files
1. Make sure that the json file is UTF-8 BOM-free. If there is a BOM, there will be garbled when reading the first line.
Per "fs.readFileSync (filename, 'utf8') doesn't strip BOM markers # 1918", fs.readFile isworking as designed: BOM is not stripped from the header of the UTF-8 file, if it exists. It at the discretion of the developer to handle this.Possible workarounds:data= data.replace (/ ^\ uFEFF/,'); per https://github.com/joyent/node/issues/1918#issuecomment-2480359Transform the incoming stream to remove the BOM header with the NPM module bomstrip per https://github.com/joyent/node/issues/1918#issuecomment-38491548What you are getting is the byte order mark header (BOM) of the UTF-8 file. When JSON.parse seesthis, it gives an syntax error (read: "unexpected character" error). You must strip the byte order mark from the file before passing it to JSON.parse:fs.readFile ('. / myconfig.json', 'utf8', function (err, data) {myconfig = JSON.parse (data.toString (' utf8'). Replace (/ ^\ uFEFF/,'));}); / / note: data is an instance of Buffer
two。 Make sure the json is not malformed. I was puzzled by the error report after I encoded it in utf8 and read the file in utf8 encoding.
Finally, it is found that json has two format errors that editor did not find, one is a missing "," between two elements in an array, and the other is a "," added at the end of the other array.
Note the iconv module of 1:Node, which only supports linux and does not support Windows, so we should use pure js iconv-lite. Another: the author says that iconv-lite has better performance. For more information, please see Git site: iconv-lite
Note 2: when I was testing to read and write files, I was always unable to write Chinese to the file. It was garbled and read normally. Later, my colleague helped me find that the encoding format of js file is that the code file of ansi,nodejs must be in utf8 format.
Note 3: if the files operated by the program are saved in UTF8 encoding format, then there is no need to use the iconv module to read the files directly in utf8 format, such as:
/ / the parameter file must be saved in utf8 format, otherwise the second parameter of function readFile (file) {/ / readFile indicates the read encoding format. If this parameter is not passed, the Buffer byte array fs.readFile (file, "utf8", function (err, data) {if (err) console.log ("read file fail" + err) is returned. Else {/ / console.log (data) when read successfully; / / output Chinese string directly});}
The problem of nodejs Reading Chinese File Encoding
Prepare a text file (csv file, etc.) test.txt and text.csv,nodejs files test.js as follows:
Var iconv = require ('iconv-lite'); var fs = require (' fs'); var fileStr = fs.readFileSync ('D:\\ test.csv', {encoding:'binary'}); var buf = new Buffer (fileStr, 'binary'); var str = iconv.decode (buf,' GBK'); console.log (str)
If you read the file directly, it is garbled. If you don't believe it, you can try it. It needs to be read uniformly with binary encoding, and then decoded with GBK.
This is the end of the content of "how to solve nodejs json Chinese garbled". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.