【问题标题】:I get "Unexpected token in JSON at position 0" when try to use readfilesync in nodejs尝试在 nodejs 中使用 readfilesync 时,我得到“位置 0 的 JSON 中的意外令牌”
【发布时间】:2019-07-03 16:45:42
【问题描述】:

我正在使用 nodejs fs.readFileSync 加载一个 json 并将其转换为 js 数组,但我收到此错误

SyntaxError: Unexpected token  in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.<anonymous> (E:\myProjects 4 98\project1\crop.js:13:18)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

但是如果我将确切的字符串作为变量保存在我的代码中,就没有错误,所以我认为我的 JSON 语法是可以的。 这是我的代码,如果我注释掉 fs.readfilesync 并取消注释下一行,问题就消失了,json.txt 和下面的字符串完全一样!

如何从外部文件加载 json?

var json = fs.readFileSync('json.txt', 'utf8');
//var json = '[{"Name":"image1.png","crops":[{"x":0,"y":53.29014916901091,"width":880,"height":502.44997787924575}]},{"Name":"image2.png","crops":[{"x":0,"y":8.616125016979977,"width":498,"height":350.3890840238524},{"x":0,"y":371.92939656630233,"width":498,"height":139.29402110784292}]},{"Name":"image3.jpg","crops":[{"x":0,"y":12.711224291602823,"width":232,"height":60.211062433908104},{"x":0,"y":65.56315687247772,"width":232,"height":109.04892418585581}]},{"Name":"image44.png","crops":[{"x":0,"y":124.83500957409233,"width":548,"height":453.5145284527152},{"x":0,"y":225.9671692290532,"width":548,"height":178.56146939079028}]}]'

这是我的 json.txt

[{"Name":"image1.png","crops":[{"x":0,"y":53.29014916901091,"width":880,"height":502.44997787924575}]},{"Name":"image2.png","crops":[{"x":0,"y":8.616125016979977,"width":498,"height":350.3890840238524},{"x":0,"y":371.92939656630233,"width":498,"height":139.29402110784292}]},{"Name":"image3.jpg","crops":[{"x":0,"y":12.711224291602823,"width":232,"height":60.211062433908104},{"x":0,"y":65.56315687247772,"width":232,"height":109.04892418585581}]},{"Name":"image44.png","crops":[{"x":0,"y":124.83500957409233,"width":548,"height":453.5145284527152},{"x":0,"y":225.9671692290532,"width":548,"height":178.56146939079028}]}]

我的整个节点应用程序!

var fs = require('fs');
var Jimp = require('jimp'); 

// User-Defined Function to read the images 
var json = fs.readFileSync('json2.txt', 'utf8');
//var json = '[{"Name":"image1.png","crops":[{"x":0,"y":53.29014916901091,"width":880,"height":502.44997787924575}]},{"Name":"image2.png","crops":[{"x":0,"y":8.616125016979977,"width":498,"height":350.3890840238524},{"x":0,"y":371.92939656630233,"width":498,"height":139.29402110784292}]},{"Name":"image3.jpg","crops":[{"x":0,"y":12.711224291602823,"width":232,"height":60.211062433908104},{"x":0,"y":65.56315687247772,"width":232,"height":109.04892418585581}]},{"Name":"image44.png","crops":[{"x":0,"y":124.83500957409233,"width":548,"height":453.5145284527152},{"x":0,"y":225.9671692290532,"width":548,"height":178.56146939079028}]}]'
// '[{"Name":"image2.png","crops":[{"x":0,"y":7.180104180816647,"width":498,"height":348.953063187689},{"x":0,"y":359.00520904083237,"width":498,"height":150.78218779714956}]},{"Name":"image4 - Copy.png","crops":[{"x":0,"y":140.63690952017996,"width":548,"height":404.5286386198435}]}]';
var array = JSON.parse(json);

 async function main() { 
     for(var i = 0; i < array.length; i++){
       var imagePath = 'E:\\myProjects 4 98\\project1\\assets\\Imgs\\' + array[i].Name;
         for (var j = 0; j < array[i].crops.length; j++){
            const image = await Jimp.read(imagePath);
            var imageName = array[i].Name.replace(/\.(.*?)$/g, '');
             var cropName = imageName + '_' + j;
            image.crop(array[i].crops[j].x, array[i].crops[j].y, array[i].crops[j].width, array[i].crops[j].height)
            .write(cropName + '.jpg'); 

         }
     }
} 
main(); 
console.log("Image Processing Completed");  


【问题讨论】:

  • 嗨!请使用tour(您将获得徽章!),环顾四周,并阅读help center,尤其是How do I ask a good question?,我还推荐Jon Skeet 的Writing the Perfect Question。你没有给我们足够的信息来回答上面的问题。假设文件确实包含有效的 JSON,那么 JSON.parse(json) 将起作用。请用minimal reproducible example 更新您的问题,以证明问题。
  • 当我用.readFileSync() 加载它并解析它时,该对象工作正常。但是,您并没有真正发布任何代码。另外,我假设该文件确实 not 包含 JSON 文本周围的单引号字符。
  • json字符串看起来不错,你用JSON.parse吗?
  • 啊,你的文件中有一个零宽度的空格字符。您可能从某个地方剪切并粘贴了它。
  • 是的,我已经从 someWhere else 中传递了它,但是这个零宽度空间在哪里?甚至使用原始的 json.txt 也会出现同样的问题

标签: javascript node.js json


【解决方案1】:

可能是您在纯文本中看不到的字符。可能是编码错误

【讨论】:

  • 当我使用 ctrl+A, ctrl + C 并将 json.txt 中的确切代码复制粘贴到我的节点应用程序并将其保存为 json 变量时,代码有效
  • 不一样
【解决方案2】:

您的 json.txt 中可能有一个 BOM。

  if (json.charCodeAt(0) === 0xfeff) {
    json = json.substr(1);
  }

添加以上行以自动删除 BOM 或仅保存不带 BOM 的文件。

【讨论】:

    猜你喜欢
    • 2021-07-01
    • 2016-12-13
    • 1970-01-01
    • 2020-02-05
    • 2021-09-12
    • 2018-10-17
    相关资源
    最近更新 更多