PowerShell如何添加到JSON数组

时间:2017-05-11 13:31:17

标签: json powershell

如何在PowerShell中添加JSON数组?我正在尝试使用以下代码,但它抱怨“集合是固定大小的”例外:

$json = @"
[
  {
    "name": "First"
  },
  {
    "name": "Second"
  }
]
"@

$toAdd =@"
{
  "name": "Third"
}
"@

$jobj = ConvertFrom-Json -InputObject $json    
$jobj.Add((ConvertFrom-Json -InputObject $toAdd))

2 个答案:

答案 0 :(得分:9)

只需使用+=代替Add()

$jobj += (ConvertFrom-Json -InputObject $toAdd)

答案 1 :(得分:0)

这有效。

$js = @"
[
  {
    "name":"First"
  },
  {
    "name":"Second"
  }
]
"@

$toAdd = @"
[
  {
    "name":"Third"
  }
]
"@

$jobj = ConvertFrom-Json -InputObject $js
$jsrc = ConvertFrom-Json -InputObject $toAdd

$jobj = $jobj + $jsrc