Powershell ConvertTo-JSON缺少嵌套级别 [英] Powershell ConvertTo-JSON missing nested level

查看:82
本文介绍了Powershell ConvertTo-JSON缺少嵌套级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果Powershell嵌套得太深,则似乎在导出为JSON时会切断数据.我的对象层次结构看起来像这样:

It looks like Powershell cuts off data when exporting to JSON if it nests too deep. My object hierchy looks like this:

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

由于某种原因,当我转换为JSON时,powershell会将第三级(这些源使用的数据集合)导出为空字符串,即使它是添加了各种NoteProperty的对象数组.例如:

For some reason, when I convert to JSON, powershell exports the Third level (Collection of data used by these sources) as an empty string, even though it is an array of objects with various NoteProperties added to them. For example:

$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字符串:

This results in the following JSON String:

PS C:\Users\user\projects\Powershell> $test | ConvertTo-Json
{
    "CollectionTest":  [
                           {
                               "ArrayTest":  " "
                           },
                           {
                               "ArrayTest":  "  "
                           }
                       ]
}

转换为XML会导致类似的情况:

Converting to XML results in a similar situation:

<?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中是否存在某种对象嵌套限制?

Is there some sort of object nesting limitation in powershell?

推荐答案

从Get-Help ConvertTo-JSON:

From Get-Help ConvertTo-JSON:

-深度< Int32>
指定JSON表示形式中包含多少级所包含的对象.默认值为2.

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

为保存数据所需的深度设置-Depth参数.

Set your -Depth parameter whatever depth you need to preserve your data.

这篇关于Powershell ConvertTo-JSON缺少嵌套级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆