我正在努力使用PowerShell脚本将新元素添加到现有JSON数组中。这是我拥有的JSON文件结构。
[
{
"Company": [
{
"Finance": [
{
"StaffName": "Name1",
"StaffRating": "Rating1"
}
],
"HR": [
{
"StaffName": "Name1",
"StaffRating": "Rating1"
},
{
"StaffName": "Name2",
"StaffRating": "Rating2"
}
]
}
]
}
]
我试图将以下对象附加到“公司”->“ HR”部分:
{
"StaffName": "Name3",
"StaffRating": "Rating3"
}
我没有运气就尝试使用'+ ='。此代码不起作用,因为 它将从文件中删除内容而不会引发任何错误:
$FromJSON = Get-Content $JsonfilePath -raw | ConvertFrom-Json
$versionDataFromJSON.Company.HR += [pscustomobject] @{ StaffName= 'Name3'},[pscustomobject] @{ StaffRating= 'Rating3' }
$FromJSON | ConvertTo-Json | Set-Content $JsonfilePath
这是我想要获取的JSON:
[
{
"Company": [
{
"Finance": [
{
"StaffName": "Name1",
"StaffRating": "Rating1"
}
],
"HR": [
{
"StaffName": "Name1",
"StaffRating": "Rating1"
},
{
"StaffName": "Name2",
"StaffRating": "Rating2"
},
{ "StaffName": "Name3",
"StaffRating": "Rating3"
}
]
}
]
}
]
答案 0 :(得分:0)
这种方式...
break