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

时间:2018-06-19 07:19:31

标签: json rest powershell

在Powershell v5上运行我的脚本时出现异常:

异常调用" DeserializeObject"用" 1"参数:"无效的JSON原语:。" 在:264 char: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上的脚本时我没有遇到此问题...

1 个答案:

答案 0 :(得分:0)

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