【发布时间】:2018-08-29 04:53:27
【问题描述】:
我在查找数组中哈希表的索引时遇到了一些麻烦。我用这段代码创建了一个 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() 方法,它应该创建输出“0”,但它给了我“-1”,因为它找不到值。如何获取“BIB1”的索引?
【问题讨论】:
-
你能展示你正在使用的对象吗?这看起来像一个 json 对象
-
请使用此信息编辑您的问题。在 cmets 中读取代码很糟糕......
-
所以。关于 ConvertTo-Json 的一件事是它不会生成“Json 对象”。它只是创建一个 Json 格式的字符串。如果您想要一个可以轻松操作和查看的结构化数据对象,我建议您将其保留为数组和哈希的哈希,就像您在转换之前一样,或者将其转换为 XML。
-
我已经试过了,没有转成json,但是IndexOf()的输出还是“-1”
-
已经尝试过什么?已经试过了怎么办?你试过什么代码?
标签: arrays powershell hashtable