PowerShell JSON:如何找出密钥是否存在=

xcitsw88  于 2023-11-20  发布在  Shell
关注(0)|答案(2)|浏览(112)

我使用Powershell 5.1,我尝试使用(简单的)JSON文件作为脚本之间的信箱。因此,在脚本的开头,我读取文件,修改它并将其存储在结尾。例如,以下文件

{
    "A":  {
              "April":  "4",
              "August":  "8"
          },
    "J":  {
              "January":  "1",
              "July":  "7"
          }
}

字符串
为了更好的处理,我写了一个Read-Value和一个Write-Value函数。在这个例子中,Read-Value显示了我的问题:函数找不到参数“Name”。

$config_file_full = Join-Path -Path $PSScriptRoot -ChildPath 'months.json'
$config_json = Get-Content -Raw -Path $config_file_full | ConvertFrom-Json

function Read-Value {
    param (
        [string]$Subject,
        [string]$Name
    )
    # $result = $null

    if (-not ($config_json.PSObject.Properties.Name -contains $Subject)) {
        Write-Host "READ NO MATCH for SUBJECT " $Subject
        $result = $null
    } elseif (-not ($config_json.PSObject.Properties.$Subject.Name -contains $Name)) {
        Write-Host "READ NO MATCH FOR SUBJECT " $Subject "NAME " $Name
        $result = $null
    } else {
        $result = $config_json.$Subject.$Name
        Write-Host "READ NO MATCH FOR SUBJECT " $Subject "NAME " $Name "VALUE <" $result ">"
    }

    return $result
}

$a = Read-Value -Subject 'A' -Name 'April'
Write-Host "<" $a ">"

$config_json | ConvertTo-Json | Set-Content -Path $config_file_full


结果是

READ NO MATCH FOR SUBJECT  A NAME  April
<  >


怎么了?其他条件不符合预期。谢谢

disbfnqx

disbfnqx1#

James Ruskin's answer将解决您的问题,但如果您将if/elseif/else语句替换为两个单独的if块,然后使用return在错误条件下“提前退出”,则可以更优雅地完成此操作:

function Read-Value {
    param (
        [string]$Subject,
        [string]$Name
    )
    if (-not ($config_json.PSObject.Properties.Name -contains $Subject)) {
        Write-Host "READ: No match for SUBJECT '$($Subject)'"
        return
    }

    # if we've reached this point it must mean the subject exists, assign to a variable
    $subjectObject = $config_json.PSObject.Properties[$Subject].Value

    if (-not ($subjectObject.PSObject.Properties.Name -contains $Name)) {
        Write-Host "READ: Matched Subject '$($Subject)' but not Name '$($Name)'"
        return
    }

    Write-Host "READ: Matched Subject '$($Subject)' and Name '$($Name):<$($result)>"
    $config_json.$Subject.$Name
}

字符串

4si2a6ki

4si2a6ki2#

这里有几个问题。

  • 这里的PSObject.Properties是一个PSPropertyInfo示例的集合(感谢@mklement0进行了更正),它不允许您使用点表示法来处理每个属性。
  • 甚至当你到达你想要检查的$Subject时,Name属性包含的是那个Subject的名字,而不是你要检查的$Name

这应该更好地工作:

function Read-Value {
    param (
        [string]$Subject,
        [string]$Name
    )
    if (-not ($config_json.PSObject.Properties.Name -contains $Subject)) {
        Write-Host "READ: No match for SUBJECT '$($Subject)'"
    } elseif (-not ($config_json.PSObject.Properties[$Subject].Value.PSObject.Properties.Name -contains $Name)) {
        Write-Host "READ: Matched Subject '$($Subject)' but not Name '$($Name)'"
    } else {
        Write-Host "READ: Matched Subject '$($Subject)' and Name '$($Name)': <$($config_json.$Subject.$Name)>"
        $config_json.$Subject.$Name
    }
}

字符串

相关问题