无法解析模板:解析 JSON 时出错:查找值开头的无效字符“ÿ”

Failed to parse template: Error parsing JSON: invalid character 'ÿ' looking for beginning of value

Failed to parse template: Error parsing JSON: invalid character 'ÿ' looking for beginning of value

我在使用

时遇到了这个错误
$Parsed_json | ConvertTo-Json -Depth 999 -Compress |
    Out-File $nameOfJsonFile -Force 

还有这个:

Failed to parse template: Error parsing JSON: invalid character 'ÿ' looking for beginning of value

使用时

$Parsed_json | ConvertTo-Json -Depth 999 | Out-File $nameOfJsonFile -Force

JSON 在线验证者批准了我的 JSON。

到目前为止,我对这个主题的研究是当您使用 Out-File 时自行打印的 Unicode 字符导致了这个问题。我的 JSON 文件的编码是 ASCII,非常感谢有关此问题的任何帮助。

显然这是一个编码问题。解决方案是使用 ASCII 编码创建输出文件(好吧,实际上它是 ANSI 编码,但由于参数参数被命名为 Ascii 为了简单起见,让我们坚持使用它),例如像这样:

$Parsed_json | ConvertTo-Json -Depth 999 -Compress |
    Out-File $nameOfJsonFile -Encoding Ascii -Force

或者像这样(Set-Content默认使用ASCII编码):

$Parsed_json | ConvertTo-Json -Depth 999 -Compress |
    Set-Content $nameOfJsonFile -Force