我正在尝试从某些输入创建嵌套数据结构并最终将其转换为 JSON,但一旦它变得太深,我就会遇到麻烦。
该结构将是一个哈希数组,其中一项是另一个哈希数组。
这设置了两个哈希数组$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"})
正如你所看到的,一切都在板上:
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
一切都呈梨形:
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
step
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:\>
真的很感谢这里的一些帮助!