带有嵌入式哈希表的 Powershell ConvertTo-json [英] Powershell ConvertTo-json with embedded hashtable

查看:34
本文介绍了带有嵌入式哈希表的 Powershell ConvertTo-json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 ConvertTo-Json 时遇到问题,并试图了解行为和/或我做错了什么.

考虑这个命令序列:

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

第一次转换给出了这个输出:

<代码>{身份证":10,配置":{身份证":11,配置":{身份证":12,配置":结束"}}}

第二次转换给出了这个输出:

<预><代码>[{身份证":10,配置":{身份证":11,配置":System.Collections.Hashtable"}}]

似乎在数组情况下转换不正确.关于为什么会发生这种情况的任何想法?

解决方案

深度有问题,默认2,可以试试:

ConvertTo-json @($val) -Depth 5

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?

解决方案

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

ConvertTo-json @($val) -Depth 5

这篇关于带有嵌入式哈希表的 Powershell ConvertTo-json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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