最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

nodejs读取本地中文json文件出现乱码解决方法

来源:懂视网 责编:小采 时间:2020-11-27 22:06:21
文档

nodejs读取本地中文json文件出现乱码解决方法

nodejs读取本地中文json文件出现乱码解决方法:1. 确定json文件是UTF-8 无BOM编码的的。如果有BOM,会在读取第一行的时候出现乱码。 Per fs.readFileSync(filename, 'utf8') doesn't strip BOM markers #1918, fs.readFile is working as designed: BOM is
推荐度:
导读nodejs读取本地中文json文件出现乱码解决方法:1. 确定json文件是UTF-8 无BOM编码的的。如果有BOM,会在读取第一行的时候出现乱码。 Per fs.readFileSync(filename, 'utf8') doesn't strip BOM markers #1918, fs.readFile is working as designed: BOM is

1. 确定json文件是UTF-8 无BOM编码的的。如果有BOM,会在读取第一行的时候出现乱码。

Per "fs.readFileSync(filename, 'utf8') doesn't strip BOM markers #1918", fs.readFile is
working 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/, ''); perhttps://github.com/joyent/node/issues/1918#issuecomment-2480359
  • Transform the incoming stream to remove the BOM header with the NPM module bomstrip perhttps://github.com/joyent/node/issues/1918#issuecomment-38491548
  • What you are getting is the byte order mark header (BOM) of the UTF-8 file. When JSON.parse sees
    this, 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
    

    2. 确定json没有格式错误。我在用utf8编码并用utf8 encoding来读取文件之后依然报错,百思不得其解。

    最后发现json有两个editor没有发现的格式错误,一个是一个数组中两个元素之间少了一个“,”,另一个是另一个数组最后多了一个“,”。

    注1:Node的iconv模块,仅支持linux,不支持Windows,因此要用纯js的iconv-lite,另:作者说iconv-lite的性能更好,具体参考Git站点:iconv-lite

    注2:我在测试读写文件时,始终无法把中文写入文件,一直乱码,读取正常,后来同事帮我发现:js文件的编码格式是ansi,nodejs的代码文件必须是utf8格式

    注3:如果程序操作的文件,都是以UTF8编码格式保存的,那么就不需要使用iconv模块,直接以utf8格式读取文件即可,如:

    // 参数file,必须保存为utf8格式,否则里面的中文会乱码  
    function readFile(file){  
        // readFile的第2个参数表示读取编码格式,如果未传递这个参数,表示返回Buffer字节数组  
        fs.readFile(file, "utf8", function(err, data){  
            if(err)  
                console.log("读取文件fail " + err);  
            else{  
                // 读取成功时  
                console.log(data);// 直接
    输出中文字符串了           }       });   }

    nodejs读取中文文件编码问题

    准备一个文本文件(当然也可以是csv文件等)test.txt和text.csv,nodejs文件test.js如下:

    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);  

    直接读文件的话是乱码,不信你可以试试。需要先统一用二进制编码方式读取,然后再用GBK解码。

    声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文档

    nodejs读取本地中文json文件出现乱码解决方法

    nodejs读取本地中文json文件出现乱码解决方法:1. 确定json文件是UTF-8 无BOM编码的的。如果有BOM,会在读取第一行的时候出现乱码。 Per fs.readFileSync(filename, 'utf8') doesn't strip BOM markers #1918, fs.readFile is working as designed: BOM is
    推荐度:
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top