gpt4 book ai didi

json - "Unexpected token in JSON at position 0"?

转载 作者:太空宇宙 更新时间:2023-11-03 23:24:12 25 4
gpt4 key购买 nike

我正在编写一个导入 JSON 文件的 node.js 模块:

const distDirPath = "c:/temp/dist/";
const targetPagePath = "c:/temp/index.html";
const cliJsonPath = "C:/CODE/MyApp/.angular-cli.json";
const fs = require('fs');

function deployAot()
{
var version = JSON.parse(fs.readFileSync(cliJsonPath, 'utf8')).version;
}

// export the module
module.exports = {
DeployAot: deployAot
};

我在 https://jsonlint.com/ 中验证了上面 json 文件的内容它是有效的 json,但是当我执行 Node 模块时,deployAot() 中上面的第一行代码返回以下错误:

"Unexpected token  in JSON at position 0"

具体的json如下:

https://jsonblob.com/cd6753d2-9e51-11e7-aa97-2f95b001b178

知道这里可能出现什么问题吗?

最佳答案

正如 @cartant 在对该问题的评论中已经提到的那样,很可能您会在文件开头得到一个特殊字符(字节顺序标记)。

我会尝试替换这个

fs.readFileSync(cliJsonPath, 'utf8')

有了这个

fs.readFileSync(cliJsonPath, 'utf8').substring(1)

删除字符串中的第一个字符,看看会发生什么。

<小时/>

GitHub 问题 fs.readFileSync(filename, 'utf8') doesn't strip BOM markers

问题的建议:

Workaround:

body = body.replace(/^\uFEFF/, '');

After reading a UTF8 file where you are uncertain whether it may have a BOM marker in it.

关于json - "Unexpected token in JSON at position 0"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46332531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com