【发布时间】:2018-05-11 15:46:27
【问题描述】:
我在 Windows PowerShell 上使用 jq 1.5 按字母顺序对 JSON 文件的字段进行排序。
到目前为止,这工作得很好,但特殊字符(如 ÜÄÖüäö)不会保留在 jq 输出中。
原始文件以UTF-8编码保存:
{
"sha": "18879fb99367924cd76d402e841155bf73c8afb3",
"commit": {
"author": {
"name": "John Doe ÜÄÖ",
"email": "john@example.com",
"date": "2017-11-23T07:51:22Z"
}
}
}
这是保存为 UTF-8 的 jq 输出:
{
"commit": {
"author": {
"date": "2017-11-23T07:51:22Z",
"email": "john@example.com",
"name": "John Doe ???"
}
},
"sha": "18879fb99367924cd76d402e841155bf73c8afb3"
}
如您所见,字符 ÜÄÖ 无法识别并保存为 ???。
这就是我在 PowerShell 中使用 jq 的方式:
$json = Get-Content .\json.txt -Encoding UTF8
$jsonSorted = $json | .\jq-win64.exe --sort-keys '.'
Set-Content jsonSorted.txt -Value $jsonSorted -Encoding UTF8
【问题讨论】:
-
正如答案中所建议的那样,在 PowerShell 中更改代码页
chcp 65001已解决此问题。但更重要的是,我还必须更改对 jq 可执行文件的调用:$jsonSorted = .\jq-win64.exe --sort-keys '.' json.txt所以不再需要使用 Get-Content cmdlet。
标签: json powershell encoding utf-8 jq