简体   繁体   English

JSON.parse(): SyntaxError: Unexpected token in JSON at position 0

[英]JSON.parse(): SyntaxError: Unexpected token � in JSON at position 0

I have a json object which returns from the following PowerShell command:我有一个 json 对象,它从以下 PowerShell 命令返回:

Get-Service -Name "name" | ConvertTo-Json -Compress > "/to/path/name.json"

If i basically open the file in vscode it seems to be correctly formatted.如果我基本上在 vscode 中打开文件,它似乎格式正确。 After i read the file with在我阅读文件后

fs.readFile(file, 'utf8', (err, data)...

and then try JSON.parse(data) im receiving the error:然后尝试JSON.parse(data)我收到错误:

undefined:1
��{
^

SyntaxError: Unexpected token � in JSON at position 0

Then i tried to do the following: data.replace(/[^\\x00-\\x7F]/g, "") to only have ASCII characters, which basically seems to work at least with a console.log().然后我尝试执行以下操作: data.replace(/[^\\x00-\\x7F]/g, "")只包含 ASCII 字符,这基本上似乎至少与 console.log() 一起工作。

But JSON.parse then complains:但是 JSON.parse 然后抱怨:

undefined:1
{
 

SyntaxError: Unexpected token  in JSON at position 1

Im not sure whats the problem there.我不确定那里有什么问题。 Hopefully somebody can help me with that.希望有人可以帮助我。

This is an example json file: As i think the format is correct.这是一个示例 json 文件:因为我认为格式是正确的。 Only too much spaces which are removed by the -Compress PowerShell parameter.只有太多的空格被-Compress PowerShell 参数删除。

{
    "CanPauseAndContinue":  false,
    "CanShutdown":  false,
    "CanStop":  false,
    "DisplayName":  "OpenSSH Authentication Agent",
    "DependentServices":  [

                          ],
    "MachineName":  ".",
    "ServiceName":  "ssh-agent",
    "ServicesDependedOn":  [

                           ],
    "ServiceHandle":  {
                          "IsInvalid":  false,
                          "IsClosed":  false
                      },
    "Status":  1,
    "ServiceType":  16,
    "StartType":  4,
    "Site":  null,
    "Container":  null,
    "Name":  "ssh-agent",
    "RequiredServices":  [

                         ]
}

Your JSON file seems to have a different encoding, maybe is utf16le instead of utf8.您的 JSON 文件似乎具有不同的编码,可能是utf16le而不是 utf8。

I replicated your scenario and found help here: Strange unicode characters when reading in file in node.js app我复制了您的场景并在此处找到了帮助: Strange unicode characters when reading in file in node.js app

fs.readFile(file, 'utf16le', (err, data)...

I think a problem occurred because of line breakers and tabs.我认为由于换行符和制表符而出现问题。 I tried to parse your code and it was parsed successfully.我试图解析你的代码,它被成功解析。 Please, try to make your JSON data to be one line.请尝试使您的 JSON 数据成为一行。 Like here: How to remove all line breaks from a string .就像这里: 如何从字符串中删除所有换行符

Maybe its the same problem as when you're writing PHP scripts that use session objects.也许它与您编写使用session对象的 PHP 脚本时存在相同的问题。

In PHP, when a file is encoded with UTF8 with BOM, the PHP script that uses session completely break.在 PHP 中,当文件使用带有 BOM 的 UTF8 编码时,使用session的 PHP 脚本完全中断。 To solve this, I generally open the file in Notepad++, goes to Format -> UTF8 (without BOM), and then save it again.为了解决这个问题,我一般在 Notepad++ 中打开文件,转到 Format -> UTF8 (without BOM),然后再次保存。 Always works for me.总是为我工作。 This might be your case here, since these broken characters appears to be on the beginning of your file, that's exacly where BOM is.这可能是您的情况,因为这些损坏的字符似乎位于文件的开头,这正是 BOM 所在的位置。

This answer might clarify about UTF8 and BOM. 这个答案可能会澄清 UTF8 和 BOM。

Normally the error message having some strange characters (like ) gives hint that there maybe some problem in the encoding.通常,包含一些奇怪字符(如 )的错误消息提示可能在编码中存在一些问题。

A few nodejs readFile api's with different encoding一些具有不同编码的 nodejs readFile api

fs.readFile(file, 'utf8', function(err, data) { ... });

fs.readFile(file, 'utf16le', function(err, data) { ... }); // le - little endian

fs.readFile(file, 'ucs2', function(err, data) { ... });   // kind  of 'utf16le'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) JSON.parse() 导致错误:`SyntaxError: Unexpected token in JSON at position 0` - JSON.parse() causes error: `SyntaxError: Unexpected token in JSON at position 0` JSON.parse() 错误 SyntaxError: Unexpected token &lt; in JSON at position 0 - JSON.parse() Error SyntaxError: Unexpected token < in JSON at position 0 语法错误:JSON 中的意外令牌 e 在 position 1 与 JSON.parse() - SyntaxError: Unexpected token e in JSON at position 1 with JSON.parse() SyntaxError: JSON JSON JSON.parse 0 中的意外标记 - SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse SyntaxError:JSON中的意外令牌u在JSON.parse( <anonymous> ) - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) 语法错误:JSON.parse 中位置 0 处的 JSON 中的意外标记 - SyntaxError: Unexpected token " in JSON at position 0 at JSON.parse Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM