Powershell转换为JSON缺少嵌套级别
如果嵌套太深,Powershell在导出为JSON时似乎会切断数据。我的对象层次结构如下所示:Powershell转换为JSON缺少嵌套级别,json,powershell,Json,Powershell,如果嵌套太深,Powershell在导出为JSON时似乎会切断数据。我的对象层次结构如下所示: Main Object Metadata More Metadata Collection of Other object Sources Collection of data used by these sources 出于某种原因,当我转换为JSON时,powershell将第三级(这些源使用的数据集合)导出为空字符串,即使它是一个添加了各种NotePro
Main Object
Metadata
More Metadata
Collection of Other object Sources
Collection of data used by these sources
出于某种原因,当我转换为JSON时,powershell将第三级(这些源使用的数据集合)导出为空字符串,即使它是一个添加了各种NoteProperties的对象数组。例如:
$test = New-Object -TypeName PSObject
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name "Name" -Value "adsf"
$test2 = New-Object -TypeName PSObject
$test2 | Add-Member -MemberType NoteProperty -Name "ArrayTest" -Value @($obj, $obj)
$test3 = New-Object -TypeName PSObject
$test3 | Add-Member -MemberType NoteProperty -Name "ArrayTest" -Value @($obj, $obj, $obj)
$test | Add-Member -MemberType NoteProperty -Name "CollectionTest" -Value @($test2, $test3)
这将产生以下JSON字符串:
PS C:\Users\user\projects\Powershell> $test | ConvertTo-Json
{
"CollectionTest": [
{
"ArrayTest": " "
},
{
"ArrayTest": " "
}
]
}
转换为XML会导致类似的情况:
<?xml version="1.0"?>
<Objects>
<Object Type="System.Management.Automation.PSCustomObject">
<Property Name="CollectionTest" Type="System.Object[]">
<Property Type="System.Management.Automation.PSCustomObject">
<Property Type="System.String">@{ArrayTest=System.Object[]}</Property>
<Property Name="ArrayTest" Type="System.Management.Automation.PSNoteProperty">System.Object[]</Property>
</Property>
<Property Type="System.Management.Automation.PSCustomObject">
<Property Type="System.String">@{ArrayTest=System.Object[]}</Property>
<Property Name="ArrayTest" Type="System.Management.Automation.PSNoteProperty">System.Object[]</Property>
</Property>
</Property>
</Object>
</Objects>
@{ArrayTest=System.Object[]}
系统对象[]
@{ArrayTest=System.Object[]}
系统对象[]
powershell中是否存在某种对象嵌套限制?从Get-Help转换到JSON: -深度
指定JSON表示中包含多少级别的包含对象。默认值为2
将-Depth参数设置为保存数据所需的任何深度。从Get-Help转换为JSON: -深度
指定JSON表示中包含多少级别的包含对象。默认值为2
将-Depth参数设置为保存数据所需的任何深度。Duh,谢谢!不知道我怎么会在帮助中错过这个!也许你们错过了它,因为它是一个奇怪的参数。这是一个完美的例子。它似乎是一个完全不必要的参数!你能想象,比方说,如果你的块嵌套超过2级,一个“C”编译器会生成糟糕的代码吗?实际效果如何,谢谢!不知道我怎么会在帮助中错过这个!也许你们错过了它,因为它是一个奇怪的参数。这是一个完美的例子。它似乎是一个完全不必要的参数!你能想象,比如说,如果你的块嵌套超过2级,一个“C”编译器会生成错误的代码吗?实际的效率是多少。