【问题标题】:JSON.parse(): SyntaxError: Unexpected token � in JSON at position 0JSON.parse(): SyntaxError: Unexpected token � in JSON at position 0
【发布时间】:2021-09-30 13:10:11
【问题描述】:

我有一个从以下 PowerShell 命令返回的 json 对象:

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

如果我基本上在 vscode 中打开文件,它似乎格式正确。 在我阅读文件后

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

然后尝试JSON.parse(data)我收到错误:

undefined:1
��{
^

SyntaxError: Unexpected token � in JSON at position 0

然后我尝试执行以下操作: data.replace(/[^\x00-\x7F]/g, "") 只有 ASCII 字符,这基本上似乎至少可以与 console.log() 一起使用。

但是 JSON.parse 然后抱怨:

undefined:1
{
 

SyntaxError: Unexpected token  in JSON at position 1

我不确定那里有什么问题。希望有人可以帮助我。

这是一个示例 json 文件:因为我认为格式是正确的。只有太多的空格被-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":  [

                         ]
}

【问题讨论】:

  • 您的 name.json 文件没有正确的语法。能发一下name.json的内容吗?
  • 这可能是 PowerShell 命令倾向于添加的 UTF-16 BOM。
  • @ÁlvaroGonzález 我该如何解决这个问题?只需将文件读取为 utf-16?

标签: javascript json powershell jsonparser


【解决方案1】:

您的 JSON 文件似乎有不同的编码,可能是 utf16le 而不是 utf8。

我复制了您的场景并在此处找到了帮助: Strange unicode characters when reading in file in node.js app

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

【讨论】:

  • 谢谢@Leo。这至少删除了行首的疯狂图标。但是我在尝试JSON.parse(data) 时仍然收到以下错误:undefined:1 { ^ SyntaxError: Unexpected token in JSON at position 0
  • 我刚发现用记事本++打开文件,编码也不是utf16le:UCS-2-LE-BOM
  • @FlorianTaut 您是否尝试使用 iconv? stackoverflow.com/a/14404102/1724128
  • 我正在阅读上面的答案,发现“不幸的是node.js只支持UTF-16 Little Endian或UTF-16LE”,也许有一种方法可以在utf-8中输出powershell json
  • 使用 iconv 我现在终于可以通过将其从 UTF18-LE 转换为 UTF-8 来解决问题。谢谢狮子座!
【解决方案2】:

我认为由于换行符和制表符而出现了问题。我尝试解析您的代码,并成功解析。请尝试将您的 JSON 数据设为一行。喜欢这里:How to remove all line breaks from a string

【讨论】:

  • 我已经这样做了。 PowerShell 的 -Compress 参数将 json 对象作为单行返回。但这并不能解决问题。
  • 尝试将 JSON 数据打印到控制台 (console.log) 您的 JSON 数据,我认为它与您的预期不同。我的测试结果:prnt.sc/1er9fvh
  • Komiljinov 如前所述,我试过它确实有效。这是使用-Compress 参数接收缩小的一个衬里时打印的内容:��{"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":[]}
【解决方案3】:

这可能与您编写使用 session 对象的 PHP 脚本时的问题相同。

在 PHP 中,当文件使用 UTF8 和 BOM 编码时,使用 session 的 PHP 脚本会完全中断。为了解决这个问题,我通常在 Notepad++ 中打开文件,转到 Format -> UTF8 (without BOM),然后再次保存。总是为我工作。这可能是您的情况,因为这些损坏的字符似乎位于文件的开头,而这正是 BOM 所在的位置。

This answer might clarify about UTF8 and BOM.

【讨论】:

    【解决方案4】:

    通常带有一些奇怪字符(如 �)的错误消息会提示编码中可能存在一些问题。

    一些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'
    

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多