gpt4 book ai didi

json - Powershell ConvertTo-JSON缺少嵌套级别

转载 作者:行者123 更新时间:2023-12-03 14:39:57 25 4
gpt4 key购买 nike

如果嵌套太深,看起来Powershell在导出为JSON时会切断数据。我的对象层次结构如下所示:

Main Object
Metadata
More Metadata
Collection of Other object Sources
Collection of data used by these sources

出于某种原因,当我转换为JSON时,powershell会将第三级(这些源使用的数据的集合)导出为空字符串,即使它是添加了各种NoteProperty的对象的数组。例如:
$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中是否存在某种对象嵌套限制?

最佳答案

从Get-Help ConvertTo-JSON:

-Depth <Int32>
Specifies how many levels of contained objects are included in the JSON representation. The default value is 2.



设置-Depth参数可以保留数据的深度。

关于json - Powershell ConvertTo-JSON缺少嵌套级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22260343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com