Powershell ConvertTo-json with embedded hashtable

前提是你 提交于 2019-12-28 02:03:47

问题


I'm having a problem with ConvertTo-Json and was trying to understand the behavior and/or what I'm doing wrong.

Consider this sequence of commands:

$val=@{ID=10;Config=@{ID=11;Config=@{ID=12;Config='end'}}}
ConvertTo-json $val
ConvertTo-json @($val)

The first conversion gives this output:

{
    "ID":  10,
    "Config":  {
                   "ID":  11,
                   "Config":  {
                                  "ID":  12,
                                  "Config":  "end"
                              }
               }
}

The second conversion gives this output:

[
    {
        "ID":  10,
        "Config":  {
                       "ID":  11,
                       "Config":  "System.Collections.Hashtable"
                   }
    }
]

It seems that in the array case the conversion is incorrect. Any ideas on why this is happening?


回答1:


It's a trouble with the depth, the default value is 2, can you try :

ConvertTo-json @($val) -Depth 5



回答2:


-Depth $([int32]::MaxValue)

specifies infinite depth (maximum possible for ConvertTo-Json cmdlet)



来源:https://stackoverflow.com/questions/17929494/powershell-convertto-json-with-embedded-hashtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!