Powershell ConvertFrom-Json编码特殊字符发行 [英] Powershell ConvertFrom-Json Encoding Special Characters Issue
问题描述
我的Powershell脚本中有此代码,在特殊字符部分上效果不佳.
I have this code in my powershell script and it doesn't do well on the special characters parts.
$request = 'http://151.80.109.18:8082/vrageremote/v1/session/players'
$a = Invoke-WebRequest -ContentType "application/json; charset=utf-8" $request |
ConvertFrom-Json |
Select -expand Data |
Select -expand players |
Select displayName, factionTag | Out-file "$scriptPath\getFactionTag.txt"
在我的输出文件中,我只会得到'????'对于任何特殊字符.有人知道我如何在输出文件中显示特殊字符吗?
In my output file I only get '????' for any special characters. Does anyone know how I can get it to show special characters in my output file?
推荐答案
Peter Schneider的有用答案和Nas的有用答案都解决了您的方法中的一个问题:您需要:
Peter Schneider's helpful answer and Nas' helpful answer both address one problem with your approach: You need to:
-
之一:访问
Invoke-WebRequest
返回的响应对象的.Content
属性,以获取返回的实际 data (作为JSON字符串),然后您可以将其传递给ConvertFrom-Json
.
either: access the
.Content
property on the response object returned byInvoke-WebRequest
to get the actual data returned (as a JSON string), which you can then pass toConvertFrom-Json
.
或:改为使用 Invoke-RestMethod
,它直接返回数据 并将其解析为自定义对象,因此您可以直接使用这些对象,而无需 ConvertTo-Json
;但是,对于这种情况下的字符编码问题,这不是 选项,因为需要对JSON字符串进行显式重新编码-参见下文.
or: use Invoke-RestMethod
instead, which returns the data directly and parses it into custom objects, so you can work with these objects directly, without the need for ConvertTo-Json
; however, with a character-encoding problem such as in this case this is not an option, because explicit re-encoding of the JSON string is needed - see below.
但是,您仍然有一个字符编码问题,因为 中缺少 charset
信息> response 标头,PowerShell解释为以
However, you still have a character-encoding problem, because, in the absence of charset
information in the response header, PowerShell interprets the UTF-8-encoded JSON string returned as ISO-8859-1-encoded (still applies as of PowerShell 7.0).
有两种可能的解决方案(除了切换到PowerShell Core 之外):
There are two possible solutions (other than switching to PowerShell Core):
-
最好将Web服务修改为在响应标头的
ContenType
字段中包含charset = utf-8
.
如果无法执行此操作,则必须明确地重新编码收到的字符串,以纠正字符编码的误解.
If you can't do that, you must explicitly re-encode the string received to correct the character-encoding misinterpretation.
这是后者的实现:
$request = 'http://151.80.109.18:8082/vrageremote/v1/session/players'
$a = Invoke-WebRequest -ContentType "application/json; charset=utf-8" $request
# $a.Content now contains the *misinterpreted* JSON string, so we must
# obtain its byte representation and re-interpret the bytes as UTF-8.
# Encoding 28591 represents the ISO-8859-1 encoding - see https://docs.microsoft.com/en-us/windows/desktop/Intl/code-page-identifiers
$jsonCorrected = [Text.Encoding]::UTF8.GetString(
[Text.Encoding]::GetEncoding(28591).GetBytes($a.Content)
)
# Now process the reinterpreted string.
$jsonCorrected |
ConvertFrom-Json |
Select -expand Data |
Select -expand players |
Select displayName, factionTag | Out-file "$scriptPath\getFactionTag.txt"
这篇关于Powershell ConvertFrom-Json编码特殊字符发行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!