将 pscustomobject 转换为 Json 格式
Converting pscustomobject into Json format
我有示例 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"
}
}
],
不是为每个数据库创建一个对象,而是创建一个对象,然后为每个数据库添加属性。
$DatabaseInfo = [pscustomobject]@{
DatabasesCount = $dblist.count
}
For($i=1;$i -le $dblist.count;$i++){
$DBObject = [pscustomobject]@{
Source = $dblist[$i-1].Name
Target = $dblist[$i-1].Name
}
$DatabaseInfo | Add-Member "DBDetails$i" $DBObject
}
我有示例 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"
}
}
],
不是为每个数据库创建一个对象,而是创建一个对象,然后为每个数据库添加属性。
$DatabaseInfo = [pscustomobject]@{
DatabasesCount = $dblist.count
}
For($i=1;$i -le $dblist.count;$i++){
$DBObject = [pscustomobject]@{
Source = $dblist[$i-1].Name
Target = $dblist[$i-1].Name
}
$DatabaseInfo | Add-Member "DBDetails$i" $DBObject
}