我正在从CSV文件中读取内容,并尝试创建类似于以下内容的JSON结构,connections属性是从3列组合而成的(即在线,镜像和共享),我将它们组合成一个数组作为JSON中的连接。我不知道为什么我没有使用数组获得正确的属性格式。
{
"connectionmap": [
{
"Environment": "abc",
"connections": ["x1", "x2", "x3"]
},
{
"Environment": "def",
"connections": ["y1"]
}
]
}
当前,我的代码是这样:
function connectionPointsGenerator {
Param(
[Parameter(Mandatory = $true)]
[string]$CsvPath,
[Parameter(Mandatory = $true)]
[string]$OutPath
)
$Json = Import-Csv $CsvPath | ConvertTo-Json
$JsonObject = $Json | ConvertFrom-Json
foreach ($env in $JsonObject) {
[array]$connections = @()
$connections += "$($env.online)"
if ($env.mirror) {
$connections += "$($env.mirror)"
}
if ($env.shared) {
$connections += "$($env.shared)"
}
Write-Output "$($connections.GetType())"
$env.PSObject.Properties.Remove("online")
$env.PSObject.Properties.Remove("mirror")
$env.PSObject.Properties.Remove("shared")
$env | Add-Member -MemberType NoteProperty -Name "connections" -Value $connections -PassThru | Out-Null
}
$hash = @{}
$hash.Add("connectionmap",$JsonObject)
$hash.connectionPointMapping.dit
$FinalJson = $hash | ConvertTo-Json | Out-File $OutPath
}
这就是我得到的:
{
"connectionmap": [
{
"Environment": "abc",
"connections": "x1 x2 x3"
},
{
"Environment": "def",
"connections": "y1"
}
]
}