我正在try 访问由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}

在这里,我想访问一个字段‘avingsCurrency’,以便在if语句中进行比较.该字段嵌套在‘addtionalInfo’中,而后者又嵌套在‘visorRecommendation’对象中.

我现在有以下代码:

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"
}

尽管我可以清楚地看到‘addtionalInfo’中的字段不是空的或空的,但‘avingsCurrency’字段从不通过NULL/Empty判断.这里出了什么问题?如果我不使用前面的存在性判断来try 打印,它只会打印附加了.avingsCurrency的添加信息对象,如下所示:

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

推荐答案

$advisorRecommendation.additionalInfo是具有嵌套属性的object:

$advisorRecommendation.additionalInfo.savingsCurrency
# USD

但是$additionalInfostring,因为在赋值之前已经将对象转换为json:

$additionalInfo = $advisorRecommendation.additionalInfo | ConvertTo-Json

$additionalInfo.GetType().FullName
# System.String

结果,$savingsCurrency = $additionalInfo.savingsCurrency试图读取stringsavingsCurrency属性,而字符串没有该属性,因此您得到$null.

相反,您要做的是保留对$advisorRecommendation.additionalInfo object的引用,并仅将其转换为json以将其写入控制台:

Write-Output "advisorrecommendation: $advisorRecommendation"

#   vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
if ($advisorRecommendation.additionalInfo -ne $null)
#   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
{

    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    $additionalInfo = $advisorRecommendation.additionalInfo
    Write-Output "additionalInfo: $($additionalInfo | ConvertTo-Json -Depth 99)"
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    if (-not([string]::IsNullOrEmpty($additionalInfo.savingsCurrency)))
    {
        $savingsCurrency = $additionalInfo.savingsCurrency
        Write-Output "Currency: $savingsCurrency"
    }
}
else
{
    $additionalInfo = $null
}

现在,您应该在输出中看到Currency: USD.

请注意,$additionalInfo.savingsCurrency已经是string,因此您不需要使用ConvertTo-Json来显示值,除非您特别希望将其格式化为json字符串(例如,"USD"带有引号和任何特殊字符,如\转义,而不是文字字符串值USD)

Json相关问答推荐

如何在PowerShell中访问嵌套的JSON字段

如何判断响应数组是否存在以及S是否有其他内容...?

数据到jsonObject到数据到 struct 是可能的吗?

使用 Powershell,如何将 Azure AD 组成员转换为 Json 对象(文件),然后可以更新?

Ansible - 将文件内容添加到字典中

展平多个数组以保持顺序

Jolt - 如何比较 if else 条件的两个值

基于JQ中另一个对象的值 Select 对象

使用 SwiftUI 在 API 调用中解码嵌套 JSON 响应时遇到问题

我无法在 Go - Gin 中解析日期/时间

在 rust 中从 API 反序列化 serde_json

解析 JSON API 响应

ASP.NET MVC - 将 Json 结果与 ViewResult 结合起来

使用 JSON 的 javascript 深拷贝

使用 GSON 解析嵌套的 JSON 数据

在 Jersey 服务中使用 JSON 对象

使用绝对或相对路径在 curl 请求中发送 json 文件

从 JSON 创建 Hashtable

严重:找不到媒体类型 = 应用程序/json、类型 = 类 com.jersey.jaxb.Todo、通用类型 = 类 com.jersey.jaxb.Todo 的 MessageBodyWriter

java.lang.BootstrapMethodError:调用站点#4 bootstrap 方法的异常,初始化 retrofit 时