我在查找数组中哈希表的索引时遇到了一些麻烦。我使用以下代码创建JSON:

$start = {
Clear-Host
$BIB = Read-Host 'Bibliothek'
$BIBName = Read-Host 'Bibliothek Name'
$Standort = Read-Host 'Bibliothek Standort'
$Autor = Read-Host 'Buchautor'
$BuchName = Read-Host 'Buchname'

$jsonfile = "C:\Skripte\bibV2-1000.xml"
if(![System.IO.File]::Exists($jsonfile)){
    $Data = @{BIBs = @(
        @{$BIB = @{BIBName=$BIBName},
        @{Standort = $Standort},
        @{Bücher = @(
            @{BuchName = $BuchName;
            Autor = $Autor})
        }}
    )}
    ConvertTo-Json -Depth 50 -InputObject $Data | Add-Content $jsonfile
    .$continue
} else {
        $jsonfile = "C:\Skripte\bibV2-1000.json"
        $Data = Get-Content $jsonfile | ConvertFrom-Json
        $Data.BIBs += New-Object -TypeName psobject -Property @{$BIB =
               @{BIBname=$BIBName},
               @{Standort=$Standort},
               @{Bücher = @(@{
                    Buchname=$BuchName;
                    Autor=$Autor})
               }
        }
        ConvertTo-Json -Depth 50 -InputObject $Data | Out-File $jsonfile}
        .$continue
}


$continue = {
Write-Host ""
Write-Host "Was wollen Sie machen?"
Write-Host "(1) Eine weitere Bibliothek hinzufügen"
Write-Host "(2) Einer Bibliothek neue Bücher hinzufügen"
Write-Host "(E) Script beenden"

    If (($read = Read-Host ) -eq "1") {
    &$start} else {
            if (($read) -eq "2") {
                . C:\Skripte\büc.ps1 } else {
                    if (($read) -eq "E") {
                        exit} else {
                            Write-Host "+++ FALSCHE EINGABE! Bitte wählen Sie (1) oder (2) für die entsprechende Aktion +++"
                            .$continue
                        }
                }
    }
}
&$start

输出如下:
{
    "BIBs": [{
        "BIB1": [{
            "BIBName": "123"
        },
            {
                "Standort": "123"
            },
            {
                "Bücher": [{
                    "Autor": "123",
                    "BuchName": "123"
                }]
            }
        ]
    },
        {
            "BIB2": [{
                "BIBname": "345"
            },
                {
                    "Standort": "345"
                },
                {
                    "Bücher": [{
                        "Autor": "345",
                        "Buchname": "345"
                    }]
                }
            ]
        }
    ]
}

现在,我想找出“BIB1”的索引。我已经尝试过IndexOf()-Method,它应该创建输出“0”,但是却给了我“-1”,因为它找不到值。如何获得“BIB1”的索引?

最佳答案

根据earlier question的判断,您正在尝试获取特定对象的索引,以便可以通过其包含数组访问它。但是,您可以直接进行以下操作:$objOfInterest = $Data.BIBs | ? BIB1-有关详细信息,请参见我的answer to your earlier question

您需要遍历$Data.BIBs的数组元素,在用ConvertFrom-Json读回序列化为JSON的JSON哈希表时,它们是自定义对象(正如Ansgar正确指出的;它们是[System.Management.Automation.PSCustomObject]的实例),并检查每个属性是否存在'BIB1':

  • (在哈希表中,您将使用'BIB1'检查是否存在密钥.ContainsKey('BIB1'))
  • 要测试对象属性的存在,您需要反射,这很容易-但有些晦涩难懂-是通过隐藏的.PSObject属性实现的,如Ansgar Wiechers' more elegant solution所示。

  • 但是,鉴于感兴趣的属性具有非空值,我们可以使用隐式 bool(boolean) 值(逻辑)从存在非空值的情况下推断给定属性存在:如果没有$obj.'BIB1'属性,默认情况下$null返回BIB1,即“falsy”之类的 bool(boolean) 值上下文,例如if条件;相反,任何非空值都是“真实的”:
    $propName = 'BIB1'
    $i = $ndx = -1
    foreach ($obj in $Data.BIBs) {
      ++$i
      if ($obj.$propName) { $ndx = $i; break}
    }
    $ndx  # $ndx now contains the index of the target object or -1 if there was no match
    

    10-05 20:07