我有示例 Json 格式,我目前正在尝试使用 powershell 进行编码并使用 PScustomobject 将其转换为 Json 格式,但是我遇到了困难,因为 DBdetails 计数将取决于我正在检索的数组。我的逻辑有什么问题?
$dbcountall = 1
foreach ($alldblist in $dblist)
{
if($dbcountall -eq 1)
{
$name = $alldblist.name
$dbdetailsname = "DBDetails" + $dbcountall
$dbpiece = [pscustomobject]@{
SourceType = "$name"
TargetType = "$name"
}
$alldb += [pscustomobject]@{
DataBasesCount = $dblist.count
$dbdetailsname = $dbpiece
}
}
else
{
$name = $alldblist.name
$dbdetailsname = "DBDetails" + $dbcountall
$dbpiece = [pscustomobject]@{
SourceType = "$name"
TargetType = "$name"
}
$alldb += [pscustomobject]@{
$dbdetailsname = $dbpiece
}
}
$dbcountall++
}
##Output is
"Databases": [
{
"DatabasesCount": "4",
"DBDetails1": {
"Source": "SampleDB1",
"Target": "SampleDB1"
}
},
{
"DBDetails2": {
"Source": "SampleDB2",
"Target": "SampleDB2"
}
},
{
"DBDetails3": {
"Source": "testdatabase",
"Target": "testdatabase"
}
},
{
"DBDetails4": {
"Source": "Testdatabase_backup_10GB",
"Target": "Testdatabase_backup_10GB"
}
}
],
结果应该看起来像这样,从上面看,它有一个额外的括号。
"Databases": [
{
"DatabasesCount": "3",
"DBDetails1": {
"Source": "db1",
"Target": "db1"
},
"DBDetails2": {
"Source": "db2",
"Target": "db2"
},
"DBDetails3": {
"Source": "db3",
"Target": "db3"
}
}
],