英文:
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't understand where'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 @( "1", "2" )
# {"nested_property":["1","2"]}
Write-Host ( $settings.nested_obj | ConvertTo-Json -Compress -Depth 100 )
# {"nested_obj":{}} - the nested object is unexpectedly empty
Write-Host ( $settings | ConvertTo-Json -Compress -Depth 100 )
Thanks
答案1
得分: 3
ConvertTo-Json
看到 $settings
中存储的对象是一个 hashtable,所以它开始序列化字典条目,而不是序列化属性 - 而在其中你没有任何字典条目。
要么将 nested_obj
和 nested_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['nested_obj'] = @{}
# add an entry to the nested hashtable
$settings['nested_obj']['nested_property'] = @( "1", "2" )
# {"nested_obj":{"nested_property":["1","2"]}}
Write-Host ( $settings | ConvertTo-Json -Compress -Depth 100 )
... or, use actual (non-dictionary) objects
# create object hierarchy
$settings = [PSCustomObject]@{
nested_obj = [PSCustomObject]@{
nested_property = @( "1", "2" )
}
}
# {"nested_obj":{"nested_property":["1","2"]}}
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 @( "1", "2" )
# {"nested_obj":{"nested_property":["1","2"]}}
Write-Host ( $settings | ConvertTo-Json -Compress -Depth 100 )
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论