Powershell Json将-with–替换为
我有一个从公共API获取数据的脚本。我尝试将Json响应中的值解析为变量。然而,当我编写Host变量时,它似乎已替换为 代码: $SetData=调用RestMethod-Urihttps://mtgjson.com/api/v5/2XM.json -ContentType应用程序/json-方法GET $Card=$SetData.data.cards |其中对象{$\.name-eq自适应自动机-和$\.isPromo-ne true} 写入主机$Card.type-ForegroundColor青色 输出:Powershell Json将-with–替换为,json,powershell,character-encoding,Json,Powershell,Character Encoding,我有一个从公共API获取数据的脚本。我尝试将Json响应中的值解析为变量。然而,当我编写Host变量时,它似乎已替换为 代码: $SetData=调用RestMethod-Urihttps://mtgjson.com/api/v5/2XM.json -ContentType应用程序/json-方法GET $Card=$SetData.data.cards |其中对象{$\.name-eq自适应自动机-和$\.isPromo-ne true} 写入主机$Card.type-ForegroundCo
工件生物–构造这里调用rest方法返回的字符串似乎编码为“ISO-8859-1”,而不是您在UTF-8中预期的那样 这意味着您需要在需要时转换为UTF-8,如下所示:
$encoding = [System.Text.Encoding]::GetEncoding('ISO-8859-1')
$SetData = Invoke-RestMethod -Uri "https://mtgjson.com/api/v5/2XM.json" -ContentType "application/json" -Method GET
$Card = $SetData.data.cards | Where-Object { $_.name -eq "Adaptive Automaton" -and !$_.isPromo}
# convert the string in '$Card.type' from encoding 'ISO-8859-1' into 'UTF-8'
$cardType = ([System.Text.Encoding]::UTF8).GetString($encoding.GetBytes($Card.type))
Write-Host $cardType -ForegroundColor Cyan
输出
Artifact Creature — Construct
这里Invoke-rest方法返回的字符串似乎是用“ISO-8859-1”编码的,而不是用UTF-8编码的 这意味着您需要在需要时转换为UTF-8,如下所示:
$encoding = [System.Text.Encoding]::GetEncoding('ISO-8859-1')
$SetData = Invoke-RestMethod -Uri "https://mtgjson.com/api/v5/2XM.json" -ContentType "application/json" -Method GET
$Card = $SetData.data.cards | Where-Object { $_.name -eq "Adaptive Automaton" -and !$_.isPromo}
# convert the string in '$Card.type' from encoding 'ISO-8859-1' into 'UTF-8'
$cardType = ([System.Text.Encoding]::UTF8).GetString($encoding.GetBytes($Card.type))
Write-Host $cardType -ForegroundColor Cyan
输出
Artifact Creature — Construct