【问题标题】:Invalid JSON primitive: . when deserializing无效的 JSON 原语:。反序列化时
【发布时间】: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-HostWrite-Verbose 转储变量的内容。

标签: json rest powershell


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多