$parent = @"
{
"Property1": "Property1Value",
"Description": "Generic Description",
"SubProperties": {
"SubSubTemplateProps": {
SubSubSubTemplateProps1 : "SubSubSubTemplateProps1",
SubSubSubTemplateProps2 : {
SubSubSubSubTemplateProps1 : [
{
"Key": "Name",
"Value": "Temp"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
}
}
}
"@ | ConvertFrom-Json
$templateobj = $parent.SubProperties.SubSubTemplateProps
$parent.SubProperties.psobject.Properties.Remove("SubSubTemplateProps")
for($j = 0; $j -lt 3; $j++)
{
$i = "{0:D2}" -f ($j+1)
$letters = @("a","b","c")
foreach($letter in $letters)
{
$newobject = $templateobj
($newobject.SubSubSubTemplateProps2.SubSubSubSubTemplateProps1 | ? {$_.key -eq "Name"}).Value = "NewValue$letter$i"
$parent.SubProperties | Add-Member -MemberType NoteProperty -Name "SubSubProp$letter$i" -Value $newobject
}
}
$parent |convertto-json -Depth 100
我期待这样的输出:
{
"Property1": "Property1Value",
"Description": "Generic Description",
"SubProperties": {
"SubSubPropa01": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValuea01"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
},
"SubSubPropb01": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValueb01"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
},
"SubSubPropa02": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValuea02"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
},
"SubSubPropb02": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValueb02"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
}
}
}
但我得到的输出是这样的,父对象的所有noteproperty都会更新,而不仅仅是我添加的noteproperty.什么
{
"Property1": "Property1Value",
"Description": "Generic Description",
"SubProperties": {
"SubSubPropa01": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValueb02"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
},
"SubSubPropb01": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValueb02"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
},
"SubSubPropa02": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValueb02"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
},
"SubSubPropb02": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValueb02"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
}
}
}
有人能建议如何复制 node 并替换子值,然后将其添加回父 node ,而不覆盖所有其他子 node 吗?不知道为什么会这样.