在将json转换为对象数组时,我遇到了一个奇怪的问题。
$response = Invoke-WebRequest -Method Get -UseDefaultCredentials -Uri $url;
$result = ConvertFrom-Json -InputObject $response
write-host $result它返回空集合(value=System.Object[]),尽管json充满了内容。
你能告诉我这个问题的可能原因和解决方法吗?
PS。当URL包含特定的关键字时,它工作得很好,并且只有在GetAll的情况下才有问题。
发布于 2019-12-02 00:25:40
通常-inputobject只能处理1个项目。它更像是管道的占位符。如果$response是一个数组,您必须这样做,这样convertfrom-json的process块才能处理所有项。
$result = $response | ConvertFrom-Json在您的示例中,唯一的问题是write-host似乎将对象数组输出为null,这似乎是一个bug或“次优行为”。
$response = ' [ {"Type": "1","Name": "First"}, {"Type": "2","Name": "Second"}, {"Type": "3","Name": "Third"} ] '
$result = convertfrom-json -inputobject $response
write-host $result
(write-host $result) -eq $null
True但是写输出或者简单的$result可以工作的很好:
$response = ' [ {"Type": "1","Name": "First"}, {"Type": "2","Name": "Second"}, {"Type": "3","Name": "Third"} ] '
$result = convertfrom-json -inputobject $response
$result
Type Name
---- ----
1 First
2 Second
3 Third发布于 2019-12-02 14:26:29
($input |转换自-Json)|选择对象
这个解决方案对我很有效
https://stackoverflow.com/questions/59125141
复制相似问题