PowerShell 的嵌套数据类型输出 [英] PowerShell's output of nested data types

查看:64
本文介绍了PowerShell 的嵌套数据类型输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从某些输入创建嵌套数据结构并最终将其转换为 JSON,但是一旦它变得太深我就会遇到麻烦.

I'm trying to create a nested data structure from some input and eventually convert it to JSON, but I am having trouble once it gets too deep.

结构将是一个散列数组,其中一项是另一个散列数组.

The structure will be an array of a hash where one item is another array of a hash.

这设置了两个散列数组 $baz1$baz2

This sets up two arrays of a hash $baz1 and $baz2

PS D:\> $baz1 = @(@{foo="foo1"; bar="bar1"};@{foo="foo2";bar="bar2"};@{foo="foo3";bar="bar3"})
PS D:\> $baz2 = @(@{foo="foo1"; bar="bar1"};@{foo="foo2";bar="bar2"};@{foo="foo3";bar="bar3"})

如您所见,一切都光明正大:

As you can see, all above board:

PS D:\> $baz1

Name                           Value
----                           -----
bar                            bar1
foo                            foo1
bar                            bar2
foo                            foo2
bar                            bar3
foo                            foo3


PS D:\> $baz1|ConvertTo-Json
[
    {
        "bar":  "bar1",
        "foo":  "foo1"
    },
    {
        "bar":  "bar2",
        "foo":  "foo2"
    },
    {
        "bar":  "bar3",
        "foo":  "foo3"
    }
]
PS D:\>

但是当我将数组添加到 $fuz 时,它会变成梨形:

But when I add the array to $fuz it all goes pear-shaped:

PS D:\> $fuz = @(@{foo="bar"; bash=$baz1};@{foo="beep";bash=$baz2})
PS D:\> $fuz

Name                           Value
----                           -----
foo                            bar
bash                           {System.Collections.Hashtable, System.Collections.Hashtable, System.Collections.Hashtable}
foo                            beep
bash                           {System.Collections.Hashtable, System.Collections.Hashtable, System.Collections.Hashtable}


PS D:\> $fuz|ConvertTo-Json
[
    {
        "foo":  "bar",
        "bash":  [
                     "System.Collections.Hashtable",
                     "System.Collections.Hashtable",
                     "System.Collections.Hashtable"
                 ]
    },
    {
        "foo":  "beep",
        "bash":  [
                     "System.Collections.Hashtable",
                     "System.Collections.Hashtable",
                     "System.Collections.Hashtable"
                 ]
    }
]

即使没有 $baz1 步骤

PS D:\> @(@{foo="bar"; bash=@(@{foo="foo1"; bar="bar1"};@{foo="foo2";bar="bar2"};@{foo="foo3";bar="bar3"})};@{foo="beep";bash=$baz2})

Name                           Value
----                           -----
foo                            bar
bash                           {System.Collections.Hashtable, System.Collections.Hashtable, System.Collections.Hashtable}
foo                            beep
bash                           {System.Collections.Hashtable, System.Collections.Hashtable, System.Collections.Hashtable}


PS D:\>

非常感谢这里的帮助!

推荐答案

使用 ConvertTo-Json cmdlet 上的 -Depth 参数来扩展子属性值.

Use the -Depth parameter on the ConvertTo-Json cmdlet to expand the child property values.

这篇关于PowerShell 的嵌套数据类型输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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