$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 吗?不知道为什么会这样.

推荐答案

如 comments 中所述,基本问题是:

  • [pscustomobject] instances, such as returned by ConvertFrom-Json are instances of a .NET reference type, which means that $newobject = $templateobj doesn't create a copy of the object stored in $templateobj in $newobject, it creates a copy of the reference to object $templateobj, so that $templateobj and $newobject end up pointing to the very same object. Only instances of .NET value types are themselves copied with simple assignments - see this answer for background information.
    Therefore, you ended up repeatedly modifying the very same object instead of independent copies of it.

  • 虽然是100 is a convenient method to create a shallow clone (copy) of a 101 instance,但这在您的情况下是不够的,因为您的实例还包含对象的property values个引用类型实例,然后这些引用类型实例又得到了references个副本.

    • 注:If you know the structure of the template object ahead of time,有simpler and more efficient solutions:

      • 使用literal [pscustomobject]对象定义inside your loop,它每次都会创建一个新实例-请参见the related answer.

      • 将模板构建为一个有序的hashtable([ordered] @{ ... }),并在循环中每次需要新副本时将其转换为[pscustomobject]——参见this answer

      • 定义一个自定义class,并在每次循环迭代中实例化它——参见this answer.


如果do需要deep-cloning of 100 graphs,请使用helper function
101
(定义如下)和-Deepswitch :

$newobject = Copy-PSCustomObject -Deep $templateobj

Helper function 100:

注意:这是一个generic深度克隆函数,可用于any类型的对象(这是不可能实现的),但它应可用于[pscustomobject]个图形,如ConvertFrom-Json返回的图形,这将它们的组成限制为:

  • 映射到基元的基元JSON类型.NET类型和[string],所有这些都是.净value types或者,在[string]的情况下,像他们一样行事.

  • Nested[pscustomobject]个相同成分的实例.

  • 其中Arrays个.

function Copy-PSCustomObject {
  [CmdletBinding()]
  param(
    [Parameter(Mandatory, ValueFromPipeline)]
    [PSCustomObject] $InputObject,
    [switch] $Deep
  )

  begin {
    if ($Deep) {
      # Helper script block (anonymous function) for walking the object graph
      $sb = {
        param($original)
        $copy = $original.psobject.Copy()
        foreach ($prop in $copy.psobject.Properties) {
          if ($prop.Value -is [System.Collections.IEnumerable]) { # Presumed array
            $prop.Value = @($prop.Value) # Clone array
            foreach ($i in 0..($prop.Value.Count-1)) {
              $prop.Value[$i] = & $sb $prop.Value[$i] # Recurse
            }
          }
          elseif ($prop.Value -is [System.Management.Automation.PSCustomObject]) {
            $prop.Value = & $sb $prop.Value # Recurse
          }
          # Otherwise: assume it is a value type or string and needs no cloning.
        }
        # Output the deep-cloned object
        $copy
      }  
    }

  }

  process {
    if ($Deep) {
      & $sb $InputObject
    }
    else {
      # Shallow copy.
      $InputObject.psobject.Copy()
    }
  }
}

Json相关问答推荐

Jolt转换问题—使用键查找匹配对象

使用Jolt将字符串数组转换为JSON对象数组

最新版本的Deneb在数据溢出时不支持滚动

JQ-JSON将键转换为对象

删除 JOLT 中的方括号

json 字符串到 Postgres 14 中的表视图

如何使用jolt规范将一个对象添加到另一个对象中并删除该对象

golang递归json来构造?

我如何将 JSON 格式与 flutter 一起使用?帮助使用 Gamebanana api

如何将动态复杂 json 解析为dart 对象或模型

在 JSON 反序列化期间没有为System.String类型定义无参数构造函数

使用 GSON 解析嵌套的 JSON 数据

我们可以使用 JSON 作为数据库吗?

gson:将 null 视为空字符串

是否可以将数据写入本地 json 文件,除了Angular 之外什么都没有?

使用 Python 3 读取 JSON 文件

将循环 struct 转换为 JSON - 有什么方法可以找到它抱怨的字段?

如果键可能不存在,则从 Python dict 读取

无法将 System.String 转换或转换为 Class 对象

用 JSON 编写 HTML 字符串