简体   繁体   English

JSON 阵列 - Powershell

[英]JSON Array - Powershell

I need an JSON array string like the following: [{"a":"1","b":"2","c“:"3","d": "4"}]我需要一个 JSON 数组字符串,如下所示: [{"a":"1","b":"2","c“:"3","d": "4"}]

If I am using the following code, I am getting {medik:[{"a":"1","b":"2","c“:"3","d": "4"}]}如果我使用以下代码,我会得到{medik:[{"a":"1","b":"2","c“:"3","d": "4"}]}

$body = @{
   medik =  @(
        @{
            a = '1'
            b = '2'
            c = '3'
            d = '4'
        }
    )
}
$jsbody=$body|ConvertTo-Json -Compress;
$jsbody 

Can anyone help me?谁能帮我? Thanks in advance!提前致谢!

Try it without using the pipeline.在不使用管道的情况下尝试一下。

When using the pipeline you are passing the array one hashtable at a time to ConvertTo-Json.使用管道时,您一次将数组一个哈希表传递给 ConvertTo-Json。 Since there is only one hashtable, it is seen as a single hashtable not an array of hashtables.由于只有一个哈希表,因此它被视为单个哈希表而不是哈希表数组。

ConvertTo-Json @(@{a = '1'; b = '2'; c = '3'; d = '4'})

Output: Output:

PS C:\tmp\overflow> $body = @(@{a = '1'; b = '2'; c = '3'; d = '4'})
>> ConvertTo-Json $body

[
  {
    "d": "4",
    "c": "3",
    "b": "2",
    "a": "1"
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM