无效的JSON基元:。反序列化时

无效的JSON基元:。反序列化时,json,rest,powershell,Json,Rest,Powershell,在Powershell v5上运行脚本时出现异常: 使用“1”参数调用“DeserializeObject”时出现异常:“无效的JSON基元:。” At:264字符:1 +$Obj=$jsonserial.DeserializeObject($Devices) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:NotSpecified:(:)[],MethodInvocationException +FullyQuali

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

使用“1”参数调用“DeserializeObject”时出现异常:“无效的JSON基元:。” At: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上的脚本时,我没有这个问题…

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

变量
$Devices
是否包含有效的json?使用
Write Host
Write Verbose
转储变量的内容。