【发布时间】:2023-02-21 20:39:29
【问题描述】:
我有一个通过Invoke-RestMethod 获得的 JSON(对象数组),我尝试使用 foreach 遍历它们。
我有兴趣用作过滤器的属性名为“$value”,我无法更改它,因为它是一个 COTS 应用程序。
foreach($item in $Result.value) {
if ($item.properties.threadType) {
Write-Host $item.properties.threadType
if ($item.properties.threadType.$value -eq "1234567") {
Write-Host $item.id
}
}
}
Write-Host $item.properties.threadType、if 语句的输出评估为 false。
@{$type=System.String; $value=18792098}
@{$type=System.String; $value=N/A}
@{$type=System.String; $value=1234567}
该 JSON 的片段:
...
"properties": {
"threadType": {
"$type": "System.String",
"$value": "1234567"
}
},
...
如何访问$value 属性?
我试过使用
$item.properties.threadType.$value$item.properties.threadType.value- 甚至
$item.properties.threadType['$value']
【问题讨论】:
-
尝试用单引号将属性名称括起来。例如
$item.properties.threadType.'$value' -eq "1234567" -
你是对的,不知道为什么我没有试过!谢谢!
标签: powershell powershell-core