简体   繁体   English

Powershell JSON 大楼

[英]Powershell JSON Building

I am having problems getting assembling this Json in Powershell我在 Powershell 中组装这个 Json 时遇到问题

{
  "totalCount": 1,
  "list": [
    {
      "type": "ToggleLightingState",
      "order": 1,
      "delay": null,
      "postDelay": null,
      "name": "Toggle lighting state of light L-17E-611-KB-1",
      "parameters": {
        "relayIds": [],
        "curveType": null,
        "behavior": null,
        "duration": null,
        "useAssignedSpace": false,
        "spaceIds": [],
        "lightIds": [
          2408
        ],
        "spaceGroupIds": []
      }
    }
  ]
}

I am Iterating through an array using a for loop to fill in the values.我正在使用 for 循环遍历数组以填充值。 I am just struggling to generate a list inside a list in JSON我只是在努力在 JSON 的列表中生成一个列表

    
    $ActionList = @{
        
    
    @(
         @{
         type = 'ToggleLightingState'
         order = 1
         delay = 'null'
         postDelay = 'null'
         name = $actionSets[$i][0]
      }
     ) 
  }    
  ConvertTo-Json -InputObject $ActionList      

You don't have the name of the array "list" inside the object. It looks like you can't have an unnamed array inside an object. I don't know what $actionsets is, so I took off the indexes. object 中没有数组“列表”的名称。看起来 object 中不能有未命名的数组。我不知道 $actionsets 是什么,所以我去掉了索引。 Plus fixing your syntax errors results in the below.再加上修复你的语法错误结果如下。 Note that 'null' and $null are different things.请注意,'null' 和 $null 是不同的东西。

$ActionList = @{
  list = @(
    @{
      type = 'ToggleLightingState'
      order = 1
      delay = 'null'
      postDelay = 'null'
      name = $actionSets
    }
  )
}
ConvertTo-Json -InputObject $ActionList


{
    "list":  [
                 {
                     "delay":  "null",
                     "name":  null,
                     "postDelay":  "null",
                     "type":  "ToggleLightingState",
                     "order":  1
                 }
             ]
}

Using thisConvertTo-Expression cmdlet:使用此ConvertTo-Expression cmdlet:

 $Json = @' { "totalCount": 1, "list": [ { "type": "ToggleLightingState", "order": 1, "delay": null, "postDelay": null, "name": "Toggle lighting state of light L-17E-611-KB-1", "parameters": { "relayIds": [], "curveType": null, "behavior": null, "duration": null, "useAssignedSpace": false, "spaceIds": [], "lightIds": [ 2408 ], "spaceGroupIds": [] } } ] } '@

$Json | ConvertFrom-Json |ConvertTo-Expression

[pscustomobject]@{
    totalCount = 1
    list = ,[pscustomobject]@{
        type = 'ToggleLightingState'
        order = 1
        delay = $Null
        postDelay = $Null
        name = 'Toggle lighting state of light L-17E-611-KB-1'
        parameters = [pscustomobject]@{
            relayIds = @()
            curveType = $Null
            behavior = $Null
            duration = $Null
            useAssignedSpace = $False
            spaceIds = @()
            lightIds = ,2408
            spaceGroupIds = @()
        }
    }
}

Or as hashtable:或者作为哈希表:

$Json |ConvertFrom-Json -AsHashTable |ConvertTo-Expression
@{
    totalCount = 1
    list = ,@{
        postDelay = $Null
        parameters = @{
            duration = $Null
            spaceGroupIds = @()
            relayIds = @()
            spaceIds = @()
            useAssignedSpace = $False
            curveType = $Null
            behavior = $Null
            lightIds = ,2408
        }
        type = 'ToggleLightingState'
        delay = $Null
        order = 1
        name = 'Toggle lighting state of light L-17E-611-KB-1'
    }
}

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

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