Json 从成功调用RestMethod中选择对象数据失败
我使用Json 从成功调用RestMethod中选择对象数据失败,json,powershell,psobject,select-object,Json,Powershell,Psobject,Select Object,我使用invokerestmethod-Uri对rest服务进行了一个工作调用https://...。调用的结果(在JSON中)可以通过管道将数据传输到Format Table-Property…,并显示数据 但是当调用后使用相同参数使用Select Object-Property…时,PSObject有列,但没有数据。如果我使用不同的Web服务,调用将起作用 是什么导致PSObject不显示任何值 公共RESTWeb服务的工作示例 Invoke-RestMethod -Uri https:
invokerestmethod-Uri对rest服务进行了一个工作调用https://...
。调用的结果(在JSON中)可以通过管道将数据传输到Format Table-Property…
,并显示数据
但是当调用后使用相同参数使用Select Object-Property…
时,PSObject
有列,但没有数据。如果我使用不同的Web服务,调用将起作用
是什么导致PSObject
不显示任何值
公共RESTWeb服务的工作示例
Invoke-RestMethod -Uri https://jsonplaceholder.typicode.com/todos/1 |
Select-Object -Property title
结果
@{title=delectus aut autem}
新故障不同API
Invoke-RestMethod -Uri https://cat-fact.herokuapp.com/facts | Select-Object -Property text
第二个示例中的问题是没有名为
text
的道具。[咧嘴笑]
唯一的道具是all
,它包含一个对象数组,其中包含一个名为text
的道具。所以你需要一些东西来获得更深的支撑。一种方法是使用两个selectobject
调用。像这样的
$Url = 'https://cat-fact.herokuapp.com/facts'
$RawIRM = Invoke-RestMethod -Uri $Url
$SO_IRM = $RawIRM |
Select-Object -ExpandProperty all |
Select-Object -Property text
$SO_IRM
var现在有一个关于猫的178个字符串数组。[grin]第二个示例中的问题是没有名为text
的道具。[咧嘴笑]
唯一的道具是all
,它包含一个对象数组,其中包含一个名为text
的道具。所以你需要一些东西来获得更深的支撑。一种方法是使用两个selectobject
调用。像这样的
$Url = 'https://cat-fact.herokuapp.com/facts'
$RawIRM = Invoke-RestMethod -Uri $Url
$SO_IRM = $RawIRM |
Select-Object -ExpandProperty all |
Select-Object -Property text
$SO_IRM
var现在有一个关于猫的178个字符串数组。[grin]在转换JSON数组时,您偶然发现了两个PowerShell古怪特性的邪恶组合:
和调用RestMethod
通过管道整体发送从Json数组转换而来的数据,而不是像往常一样逐个元素发送:ConvertFrom Json
- 注意:在PowerShell(Core)7.0中,
的行为被更改以与通常的元素枚举行为保持一致,并且在旧行为中添加了一个ComvertFrom Json
开关作为选项加入。有关导致此更改的讨论,请参阅-NoEnumerate
- 但是,在撰写本文时(PowerShell(Core 7.2)
仍然显示出这种意外行为,这在中进行了讨论调用RestMethod
- 注意:在PowerShell(Core)7.0中,
不执行,因此它直接在数组中不存在的位置查找指定的属性(例如Select Object
)text
# Windows PowerShell only:
# Because ConvertFrom-Json sends an *array* (of 2 custom objects) through
# the pipeline, Select-Object looks for property .text on the *array* -
# and can't find it.
# The same applies to Invoke-RestMethod (also still in
# PowerShell (Core) as of v7.2)
PS> ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]' | Select-Object text
text
----
# NO VALUES
一个简单的解决方法是将convertfromjson
/Invoke RestMethod
调用包含在(…)
中,这将强制数组枚举,从而使选择对象
按预期工作:
# (...) forces enumeration
PS> (ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]') | Select-Object text
text
----
a
b
请注意,Select Object-Property text
(不带-ExpandProperty
)等命令仍然输出具有.text
属性的自定义对象,而不是.text
属性值
如果您只对属性值感兴趣,则解决方案更简单,因为您可以直接在数组上使用上述成员枚举:
# Using .<propName> on an array (collection) implicitly returns the
# property values from the *elements* of that collection (member enumeration).
PS> (ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]').text
a
b
#在数组(集合)上使用.on隐式返回
#来自该集合的*元素*的属性值(成员枚举)。
PS>(从Json'[{“text”:“a”},{“text”:“b”}]'转换而来)。text
A.
B
请注意,现在输出没有
文本
标题,因为输出的仅仅是字符串值,而不是自定义对象。在转换JSON数组时,您偶然发现了两个PowerShell奇怪之处的邪恶组合:
和调用RestMethod
通过管道整体发送从Json数组转换而来的数据,而不是像往常一样逐个元素发送:ConvertFrom Json
- 注意:在PowerShell(Core)7.0中,
的行为已更改以与通常的元素枚举行为保持一致,并添加了一个ComvertFrom Json
开关作为旧行为的一个选项。有关导致此更改的讨论,请参阅-NoEnumerate
- 但是,在撰写本文时(PowerShell(Core 7.2)
仍然显示出这种意外行为,这在中进行了讨论调用RestMethod
- 注意:在PowerShell(Core)7.0中,
不执行,因此它直接在数组中不存在的位置查找指定的属性(例如Select Object
)text
# Windows PowerShell only:
# Because ConvertFrom-Json sends an *array* (of 2 custom objects) through
# the pipeline, Select-Object looks for property .text on the *array* -
# and can't find it.
# The same applies to Invoke-RestMethod (also still in
# PowerShell (Core) as of v7.2)
PS> ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]' | Select-Object text
text
----
# NO VALUES
一个简单的解决方法是将convertfromjson
/Invoke RestMethod
调用包含在(…)
中,这将强制数组枚举,从而使选择对象
按预期工作:
# (...) forces enumeration
PS> (ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]') | Select-Object text
text
----
a
b
请注意,Select Object-Property text
(不带-ExpandProperty
)等命令仍然输出具有.text
属性的自定义对象,而不是.text
属性值
如果您只对属性值感兴趣,则解决方案更简单,因为您可以直接在数组上使用上述成员枚举:
# Using .<propName> on an array (collection) implicitly returns the
# property values from the *elements* of that collection (member enumeration).
PS> (ConvertFrom-Json '[{ "text": "a" }, { "text": "b" }]').text
a
b
#在数组(集合)上使用.on隐式返回
#来自该集合的*元素*的属性值(成员枚举)。
PS>(从Json'[{“text”:“a”},{“text”:“b”}]'转换而来)。text
A.
B
请注意,现在输出没有
文本
标题,因为输出的只是字符串值,而不是自定义对象。选择对象返回带有空道具的对象最常见的原因是使用了错误的名称