看起来Powershell在导出到JSON时,如果数据嵌套太深,就会切断数据。我的对象层次结构如下所示:
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>powershell中是否存在某种对象嵌套限制?
发布于 2014-03-08 05:02:36
从Get-Help ConvertTo-JSON:
-Depth
指定在JSON表示中包含多少级别的包含对象。默认值为2。
设置您的-Depth参数,无论您需要保留数据的深度。
https://stackoverflow.com/questions/22260343
复制相似问题