简体   繁体   English

PowerShell创建嵌套JSON

[英]PowerShell create nested JSON

I'm trying to create a JSON object in PowerShell so I can use it in a python script. 我正在尝试在PowerShell中创建JSON对象,以便可以在python脚本中使用它。 I've had mixed results, I'm trying to compress this into one command so I can just run in from my python script 我的结果参差不齐,我试图将其压缩为一个命令,以便可以从python脚本中运行

ForEach ($disk in (Get-Partition).DiskNumber | select -Unique | Sort-Object)`
{ConvertTo-Json @{$disk.ToString()=`
ForEach ($part in (Get-Partition -DiskNumber $disk).PartitionNumber | Sort-Object)`
{@{$disk.ToString()=Get-Partition -DiskNumber $disk -PartitionNumber $part | `
ForEach-Object {ConvertTo-Json @{PartitionNumber=$_.PartitionNumber;DriveLetter=$_.DriveLetter;Offset=$_.Offset`
}}}}}}

This is the output I get: 这是我得到的输出:

{
    "0":  [
              {
                  "1":  "{\r\n    \"DriveLetter\":  null,\r\n    \"Offset\":  17408,\r\n    \"PartitionNumber\":  1\r\n}"
              },
              {
                  "2":  "{\r\n    \"DriveLetter\":  \"F\",\r\n    \"Offset\":  135266304,\r\n    \"PartitionNumber\":  2\r\n}"
              }
          ]
}
{
    "1":  [
              {
                  "1":  "{\r\n    \"DriveLetter\":  null,\r\n    \"Offset\":  1048576,\r\n    \"PartitionNumber\":  1\r\n}"
              },
              {
                  "2":  "{\r\n    \"DriveLetter\":  null,\r\n    \"Offset\":  315621376,\r\n    \"PartitionNumber\":  2\r\n}"
              },
              {
                  "3":  "{\r\n    \"DriveLetter\":  null,\r\n    \"Offset\":  419430400,\r\n    \"PartitionNumber\":  3\r\n}"
              },
              {
                  "4":  "{\r\n    \"DriveLetter\":  \"C\",\r\n    \"Offset\":  553648128,\r\n    \"PartitionNumber\":  4\r\n}"
              }
          ]
}
{
    "2":  [
              {
                  "1":  "{\r\n    \"DriveLetter\":  null,\r\n    \"Offset\":  17408,\r\n    \"PartitionNumber\":  1\r\n}"
              },
              {
                  "2":  "{\r\n    \"DriveLetter\":  \"D\",\r\n    \"Offset\":  135266304,\r\n    \"PartitionNumber\":  2\r\n}"
              }
          ]
}

The problem is that ConvertTo-Json creates a list/array inside the first key and a dictionary for each object inside that list, is there any way to overcome this? 问题在于,ConvertTo-Json在第一个键内创建一个列表/数组,并为该列表内的每个对象创建一个字典,有什么办法可以克服这个问题? The newline characters I can remove so that's not a big issue. 我可以删除换行符,所以这不是大问题。 Here's how I want it to look: 这是我想要的样子:

{
        "0":
            "1":  {"DriveLetter": null, "Offset": 17408, "PartitionNumber": 1},
            "2":  {"DriveLetter": "F", "Offset": 135266304, "PartitionNumber": 2}
    }
    {
        "1":
            "1":  {"DriveLetter": null, "Offset": 1048576, "PartitionNumber":  1},
            "2":  {"DriveLetter": null, "Offset": 315621376, "PartitionNumber":  2},
            "3":  {"DriveLetter": null, "Offset": 419430400, "PartitionNumber":  3},
            "4":  {"DriveLetter": "C", "Offset": 553648128, "PartitionNumber":  4}
    }
    {
        "2": 
            "1":  {"DriveLetter": null, "Offset": 17408, "PartitionNumber": 1},
            "2":  {"DriveLetter": "D", "Offset": 135266304, "PartitionNumber": 2}
    }

There are currently 3 issues with your code. 您的代码当前存在3个问题。

First, you call ConvertTo-Json (at least) twice - once on the inner Partition hashtable and then again on the outer hashtable. 首先,(至少)两次调用ConvertTo-Json一次在内部Partition哈希表上,然后再次在外部hashtable上。

Second, you haven't specified the Depth , meaning that the inner dictionaries will be collapsed. 其次,您没有指定Depth ,这意味着内部字典将被折叠。

Lastly, you should use the partition number $part as the key of the second level of dictionaries if you want the output you describe: 最后,如果您想要描述的输出,则应使用分区号$part作为第二级字典的键:

foreach ($disk in (Get-Partition).DiskNumber | select -Unique | Sort-Object){
  ConvertTo-Json -Depth 3 @{
    "$disk" = foreach ($part in (Get-Partition -DiskNumber $disk).PartitionNumber | Sort-Object){
      @{
        "$part" = Get-Partition -DiskNumber $disk -PartitionNumber $part | ForEach-Object {
          @{
            PartitionNumber = $_.PartitionNumber;
            DriveLetter     = $_.DriveLetter;
            Offset          = $_.Offset
          }
        }
      }
    }
  }
}

The above will produce n discrete JSON structures for n disks. 上面将为n n磁盘生成n离散的JSON结构。


Move ConvertTo-Json outside the entire expression if you want a single structure with the disk array as the root object: 如果要使用磁盘阵列作为根对象的单个结构,请将ConvertTo-Json整个表达式之外:

ConvertTo-Json -Depth 4 @(foreach ($disk in (Get-Partition).DiskNumber | select -Unique | Sort-Object){
  @{
    "$disk" = foreach ($part in (Get-Partition -DiskNumber $disk).PartitionNumber | Sort-Object){
      @{
        "$part" = Get-Partition -DiskNumber $disk -PartitionNumber $part | ForEach-Object {
          @{
            PartitionNumber = $_.PartitionNumber;
            DriveLetter     = $_.DriveLetter;
            Offset          = $_.Offset
          }
        }
      }
    }
  }
})

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

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