PowerShell ConvertTo-Json无法按预期方式转换数组 [英] PowerShell ConvertTo-Json does not convert Array as expected

查看:375
本文介绍了PowerShell ConvertTo-Json无法按预期方式转换数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理具有嵌套数组的JSON对象.以下PowerShell命令:

I am trying to manipulate JSON object that has an array nested. The following PowerShell commands:

@{testArray=@(1,2)} | ConvertTo-Json -Compress
@{testArray=@(@{prop1=1})} | ConvertTo-Json -Compress
@{testArray=@(@{prop1=@(1,2)})} | ConvertTo-Json -Compress

产生以下输出:

{"testArray":[1,2]}
{"testArray":[{"prop1":1}]}
{"testArray":[{"prop1":"1 2"}]}

前两个可以达到我的期望,但最后一个却不.我希望输出:

The first two do what I would expect but the last one doesn't. I would expect output:

{"testArray":[{"prop1":[1,2]}]}

我在这里想念什么?使用PowerShell 5.1

What am I missing here? Using PowerShell 5.1

推荐答案

您需要为ConvertTo-Json指定深度

You would need to specify the depth for ConvertTo-Json

 @{testArray=@(@{prop1=@(1,2)})} | ConvertTo-Json -Depth 3 -Compress

会返回

{"testArray":[{"prop1":[1,2]}]}

这篇关于PowerShell ConvertTo-Json无法按预期方式转换数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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