ConvertTo-Json 忽略了内部对象中的数组。

huangapple go评论67阅读模式
英文:

ConvertTo-Json ignores array in the inner object

问题

我有一个需要扩展的对象。问题是在转换为JSON后,内部数组对象总是空的。我不明白哪里出错了 :(

```powershell
$settings = @{}

# 创建嵌套对象
$settings | Add-Member -Type NoteProperty -Name nested_obj -Value @{}

# 使用数组属性扩展嵌套对象
$settings.nested_obj | Add-Member -Type NoteProperty -Name nested_property -Value @("1", "2")

# {"nested_property":["1","2"]}
Write-Host ($settings.nested_obj | ConvertTo-Json -Compress -Depth 100)

# {"nested_obj":{}} - 嵌套对象意外地为空
Write-Host ($settings | ConvertTo-Json -Compress -Depth 100)

谢谢


<details>
<summary>英文:</summary>

I have an object I need to extend. The issue is after converting to JSON the inner array object is always empty. I don&#39;t understand where&#39;s my fail :(

```powershell
$settings = @{}

# create the nested object
$settings | Add-Member -Type NoteProperty -Name nested_obj -Value @{}

# extend the nested object with an array property
$settings.nested_obj | Add-Member -Type NoteProperty -Name nested_property -Value @( &quot;1&quot;, &quot;2&quot; )

# {&quot;nested_property&quot;:[&quot;1&quot;,&quot;2&quot;]}
Write-Host ( $settings.nested_obj | ConvertTo-Json -Compress -Depth 100 )

# {&quot;nested_obj&quot;:{}} - the nested object is unexpectedly empty
Write-Host ( $settings | ConvertTo-Json -Compress -Depth 100 )

Thanks

答案1

得分: 3

ConvertTo-Json 看到 $settings 中存储的对象是一个 hashtable,所以它开始序列化字典条目,而不是序列化属性 - 而在其中你没有任何字典条目。

要么将 nested_objnested_property 值附加为字典条目:

# 创建根哈希表/字典
$settings = @{}

# 创建嵌套哈希表
$settings['nested_obj'] = @{}

# 向嵌套哈希表添加条目
$settings['nested_obj']['nested_property'] = @( "1", "2" )

# {"nested_obj":{"nested_property":["1","2"]}}
Write-Host ( $settings | ConvertTo-Json -Compress -Depth 100 )

... 或者,使用实际的(非字典)对象:

# 创建对象层次结构
$settings = [PSCustomObject]@{
  nested_obj = [PSCustomObject]@{
    nested_property = @( "1", "2" )
  }
}

# {"nested_obj":{"nested_property":["1","2"]}}
Write-Host ( $settings | ConvertTo-Json -Compress -Depth 100 )

PowerShell 2.0 版本中,使用 Add-Member,与您的初始尝试相似,不同之处在于我们用 New-Object psobject 替换了 @{}

$settings = New-Object psobject
$settings | Add-Member -Type NoteProperty -Name nested_obj -Value (New-Object psobject)
$settings.nested_obj | Add-Member -Type NoteProperty -Name nested_property -Value @( "1", "2" )

# {"nested_obj":{"nested_property":["1","2"]}}
Write-Host ( $settings | ConvertTo-Json -Compress -Depth 100 )
英文:

ConvertTo-Json sees the object stored in $settings is a hashtable, and so rather than serializing properties, it starts serializing dictionary entries - of which you have none.

Either attach the nested_obj and nested_property values as dictionary entries:

# create root hashtable/dictionary
$settings = @{}

# create the nested hashtable
$settings[&#39;nested_obj&#39;] = @{}

# add an entry to the nested hashtable
$settings[&#39;nested_obj&#39;][&#39;nested_property&#39;] = @( &quot;1&quot;, &quot;2&quot; )

# {&quot;nested_obj&quot;:{&quot;nested_property&quot;:[&quot;1&quot;,&quot;2&quot;]}}
Write-Host ( $settings | ConvertTo-Json -Compress -Depth 100 )

... or, use actual (non-dictionary) objects

# create object hierarchy
$settings = [PSCustomObject]@{
  nested_obj = [PSCustomObject]@{
    nested_property = @( &quot;1&quot;, &quot;2&quot; )
  }
}

# {&quot;nested_obj&quot;:{&quot;nested_property&quot;:[&quot;1&quot;,&quot;2&quot;]}}
Write-Host ( $settings | ConvertTo-Json -Compress -Depth 100 )

The PowerShell 2.0-equivalent, using Add-Member, would be fairly similar to your initial attempt, with the difference that we replace @{} with New-Object psobject:

$settings = New-Object psobject
$settings | Add-Member -Type NoteProperty -Name nested_obj -Value (New-Object psobject)
$settings.nested_obj | Add-Member -Type NoteProperty -Name nested_property -Value @( &quot;1&quot;, &quot;2&quot; )

# {&quot;nested_obj&quot;:{&quot;nested_property&quot;:[&quot;1&quot;,&quot;2&quot;]}}
Write-Host ( $settings | ConvertTo-Json -Compress -Depth 100 )

huangapple
  • 本文由 发表于 2023年6月14日 23:24:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定