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

查看:23
本文介绍了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 将第三级(这些源使用的数据的集合)导出为空字符串,即使它是一个添加了各种 NoteProperties 的对象数组.例如:

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:UsersuserprojectsPowershell> $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:

-深度
指定包含在 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天全站免登陆