我在将哈希表添加到多维数组时遇到问题。我编码如下:
$Data = @{BIBs = @(
@{$BIB = @{BIBName=$BIBName},
@{Standort = $Standort},
@{Bücher = @(
@{BuchName = $BuchName;
Autor = $Autor
})
}}
)}
此代码正在运行并创建一个输出,我将其存储在 JSON 中:
{
"BIBs": [
{
"BIB1": [
{
"BIBName": "123"
},
{
"Standort": "123"
},
{
"Bücher": [
{
"Autor": "123",
"BuchName": "123"
}
]
}
]
},
{
"BIB2": [
{
"BIBname": "345"
},
{
"Standort": "345"
},
{
"Bücher": [
{
"Autor": "345",
"Buchname": "345"
}
]
}
]
}
]
}
我有额外的代码,将另一个哈希表添加到数组“BIB”中,如您所见。
$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})}
}
当输出如上所示时,我无法向“Bücher”添加另一个哈希表。我检查了“Bücher”的类型
$data.BIBs.BIB1.Bücher.GetType()
它实际上是一个数组:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
I tried
$Data.BIBs.BIB1.Bücher += @{Person="Max";Alter="35"}
添加一个新的哈希表,就像我对“BIB2”所做的那样,但我收到错误:
The property 'Bücher' cannot be found on this object. Verify that the property
exists and can be set.
At line:5 char:1
+ $data.BIBs.BIB1.Bücher += @{Motor="asdf";pers="345"}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
你知道我怎么添加吗@{Person="Max";Alter="35"}
到“布歇尔”?