我正在尝试访问Azure Advisor使用PowerShell创建的JSON对象中的特定字段。我有以下对象:
@{id=/subscriptions/xxxx/providers/Microsoft.Advisor/recommendations/xxxx;
category=Cost;
impact=High;
impactedArea=Microsoft.Subscriptions/subscriptions;
description=Consider virtual machine reserved instance to save over your on-demand costs;
recommendationText=Consider virtual machine reserved instance to save over your on-demand costs;
recommendationTypeId=xxx; instanceName=xxxx;
additionalInfo=;
tags=;
ResourceId=/subscriptions/xxxx/providers/Microsoft.Advisor/recommendations/xxxx}
字符串
在这里,我想访问一个字段“savingsCurrency”以在if语句中进行比较。该字段嵌套在“additionalInfo”中,而“additionalInfo”又在“AdditionalRecommendation”对象中。
我现在有以下代码:
Write-Output "advisorrecommendation: $advisorRecommendation"
if (-not([string]::IsNullOrEmpty($advisorRecommendation.additionalInfo)))
{
$additionalInfo = $advisorRecommendation.additionalInfo | ConvertTo-Json
Write-Output "additionalInfo: $additionalInfo"
if (-not([string]::IsNullOrEmpty($additionalInfo.savingsCurrency)))
{
$savingsCurrency = $additionalInfo.savingsCurrency | ConvertTo-Json
Write-Output "Currency: $savingsCurrency"
}
}
else
{
$additionalInfo = $null
}
型
这段代码给出了以下输出:
advisorrecommendation:
@{id=/subscriptions/xxxx/providers/Microsoft.Advisor/recommendations/xxx; category=Cost; impact=High; impactedArea=Microsoft.Subscriptions/subscriptions; description=Consider virtual machine reserved instance to save over your on-demand costs; recommendationText=Consider virtual machine reserved instance to save over your on-demand costs; recommendationTypeId=xxxx; instanceName=xxxx; additionalInfo=; tags=; ResourceId=/subscriptions/xxxx/providers/Microsoft.Advisor/recommendations/xxxx}
additionalInfo: {
"annualSavingsAmount": "697",
"reservedResourceType": "virtualmachines",
"targetResourceCount": "4",
"savingsCurrency": "USD",
"savingsAmount": "58",
"lookbackPeriod": "30",
"displaySKU": "Standard_B2s",
"displayQty": "4",
"location": "westeurope",
"region": "westeurope",
"vmSize": "Standard_B2s",
"subId": "xxxx",
"scope": "Single",
"term": "P1Y",
"qty": "4",
"sku": "Standard_B2s"
}
的字符串
“savingsCurrency”字段永远不会通过null/empty检查,即使我可以清楚地看到“additionalInfo”中的字段不是空的或null。这里出了什么问题?如果我在没有前面的存在检查的情况下尝试打印,它只是打印additionalInfo对象,并像这样追加.savingsCurrency:
savingsCurrency: {
"annualSavingsAmount": "697",
"reservedResourceType": "virtualmachines",
"targetResourceCount": "4",
"savingsCurrency": "USD",
"savingsAmount": "58",
"lookbackPeriod": "30",
"displaySKU": "Standard_B2s",
"displayQty": "4",
"location": "westeurope",
"region": "westeurope",
"vmSize": "Standard_B2s",
"subId": "xxxx",
"scope": "Single",
"term": "P1Y",
"qty": "4",
"sku": "Standard_B2s"
}.savingsCurrency
型
2条答案
按热度按时间c0vxltue1#
$advisorRecommendation.additionalInfo
是一个带有嵌套属性的 object:字符串
但是
$additionalInfo
是一个 string,因为你在赋值之前已经将对象转换为json:型
因此,
$savingsCurrency = $additionalInfo.savingsCurrency
试图读取 string 的savingsCurrency
属性,而字符串没有该属性,因此您得到$null
。你想做的是保留对
$advisorRecommendation.additionalInfo
object 的引用,并只将其转换为json,以便将其写入控制台:型
您现在应该在输出中看到
Currency: USD
。注意,
$additionalInfo.savingsCurrency
已经是一个 string,所以你不需要使用ConvertTo-Json
来显示这个值,除非你特别希望它被格式化为一个json字符串(即"USD"
带有引号和任何特殊字符,如\
转义,而不是字面字符串值USD
)9o685dep2#
“savingsCurrency”字段从未通过null/empty检查,即使我可以清楚地看到“additionalInfo”中的字段不是空的或null。这里出了什么问题?
您可以检查not null和if条件如下:
字符串
x1c 0d1x的数据
如何在PowerShell中访问JSON字段
要访问任何json文件,您需要首先将其从json转换为如下所示:
型
要访问任何字段,您可以使用点运算符:
型
的