Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

Powershell ConvertTo-json with embedded hashtable

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

ConvertTo-json @($val) -Depth 5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...