无效的 JSON 原语:反序列化时



我在Powershell v5上运行脚本时遇到异常:

使用"1"参数调用"反序列化对象"的异常:"无效的 JSON 原语:"。 在 :264 字符:1 + $Obj = $jsonserial。反序列化对象($Devices( + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 类别信息:未指定:(:) [],方法调用异常 + 完全限定的错误 ID : 参数异常

下面的一些脚本...我错过了什么?

$useJSON = "application/json"
$headers = Build-Headers $restUserName $tenantAPIKey $useJSON $useJSON
$DeleteTime = (Get-Date).AddDays(-35)
$Devices = Invoke-RestMethod -Method GET -Uri $endpointURL -Headers $headers -TimeoutSec 30
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")        
$jsonserial= New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer 
$jsonserial.MaxJsonLength  = [Int32]::MaxValue
$Obj = $jsonserial.DeserializeObject($Devices)
$Devices = $Obj.Devices

我想补充一点,仅在 v4 上在 powershell v5 上运行脚本时我没有这个问题......

Invoke-RestMethod 已经从 JSON 转换了您的服务器响应。如果您想自己做,请尝试 Invoke-WebRequest。

最新更新