【发布时间】:2018-06-20 04:48:43
【问题描述】:
在 Powershell v5 上运行我的脚本时遇到异常:
使用“1”参数调用“DeserializeObject”的异常:“无效的 JSON 原语:。” 在:264 字符:1 + $Obj = $jsonserial.DeserializeObject($Devices) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentException
下面的一些脚本......我错过了什么?
$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
我想补充一点,仅在 v5 上运行 powershell v4 上的脚本时我没有这个问题...
【问题讨论】:
-
$Devices变量是否包含有效的 json?使用Write-Host或Write-Verbose转储变量的内容。
标签: json rest powershell