意外数组字符串的转换 [英] Unexpected array to string conversion
问题描述
我试图用显示之前收拾我的数据为对象的ConvertTo JSON的
。下面的测试案例展示完美我是如何处理的数据,什么问题时:
I'm trying to pack my data into objects before displaying them with ConvertTo-Json
. The test case below shows perfectly how I'm dealing with data and what problem occurs:
$array = @("a","b","c")
$data = @{"sub" = @{"sub-sub" = $array}}
$output = @{"root" = $data}
ConvertTo-Json -InputObject $data
ConvertTo-Json -InputObject $output
输出(由专人为清楚起见格式化):
Output (formatted by hand for clarity):
{ "sub": { "sub-sub": [ "a", "b", "c" ] }}
{ "root": { "sub": { "sub-sub": "a b c" } }}
有什么办法来分配 $数据
到 $输出
没有这个奇怪的隐式转换?
Is there any way to assign $data
to $output
without this weird implicit casting?
推荐答案
由于在评论中提到的,的ConvertTo JSON的
将尝试的平展的对象结构超出最大嵌套级别,或深度的,通过将它找到的任何对象超出深度为一个字符串。
As mentioned in the comments, ConvertTo-Json
will try to flatten the object structure beyond a maximum nesting level, or depth, by converting whatever object it finds beyond that depth to a string.
默认深度为2,但你可以指定它应该去更深的深度
参数:
The default depth is 2, but you can specify that it should go deeper with the Depth
parameter:
PS C:\> @{root=@{level1=@{level2=@("level3-1","level3-2")}}}|ConvertTo-Json
{
"root": {
"level1": {
"level2": "level3-1 level3-2"
}
}
}
PS C:\> @{root=@{level1=@{level2=@("level3-1","level3-2")}}}|ConvertTo-Json -Depth 3
{
"root": {
"level1": {
"level2": [
"level3-1",
"level3-2"
]
}
}
}
这篇关于意外数组字符串的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!