Powershell json format



我有一个有关使用PowerShell转换为JSON的问题。我需要具有特定格式的JSON(用于休息(,但是如果我进行转换并转换为json,则格式是错误的,我不知道如何更改它。这是原始格式:

{
    "property-content":  [
                             {
                                 "statKey":  "Test|Test1",
                                 "timestamps":  [1522678260000],
                                 "values":  ["compliant"],
                                 "others":  "",
                                 "otherAttributes":  ""
                             }
                         ]
}

,如果我从converm-json进行convert ...然后我更改一些值,然后进行转换,则格式没有括号:

{
    "property-content":  [
                             {
                                 "statKey":  "Test|Test1",
                                 "timestamps":  "1522678260000",
                                 "values":  "compliant",
                                 "others":  "",
                                 "otherAttributes":  ""
                             }
                         ]
}

谢谢!

由于ConvertTo-Json CMDLET将数组扁平,因为-Depth参数默认设置为2。如果您更高的-Depth将解决您的问题:

PS C:> $a = ConvertFrom-JSON @'
>> {
>>     "property-content":  [
>>                              {
>>                                  "statKey":  "Test|Test1",
>>                                  "timestamps":  [1522678260000],
>>                                  "values":  ["compliant"],
>>                                  "others":  "",
>>                                  "otherAttributes":  ""
>>                              }
>>                          ]
>> }
>> '@
PS C:> $a | convertTo-JSON -Depth 5
{
    "property-content":  [
                             {
                                 "statKey":  "Test|Test1",
                                 "timestamps":  [
                                                    1522678260000
                                                ],
                                 "values":  [
                                                "compliant"
                                            ],
                                 "others":  "",
                                 "otherAttributes":  ""
                             }
                         ]
}

最新更新