遍历JSON数组并打印出相同的键值

时间:2019-07-17 19:34:46

标签: arrays powershell epoch

具有多个具有相同键的JSON数组,该键显示时期,要打印然后输出并将时间转换为EST

{
"contents":[
      { 
        "name":"windows-Instance",
        "Buildid":"1234",
        "Buildtime":"1563350400238"
      },
      {
        "name":"linux-Instance",
        "Buildid":"1454",
        "Buildtime":"1563264000198"
      },
      {
      "name":"linux-Instance",
        "Buildid":"1278685",
        "Buildtime":"1563177600092"
       }
  ]
}

1 个答案:

答案 0 :(得分:0)

您可以尝试将每个将Epoch转换为当前时区的对象打印出来:

Function Get-EpochDate ($epochDate) { [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddMilliseconds($epochDate)) }

$contents = (Get-Content .\test.json | ConvertFrom-Json).contents

foreach ($object in $contents) {
    $seconds = [long]$object.Buildtime
    Write-Output (Get-EpochDate $seconds)
}

输出:

Wednesday, 17 July 2019 4:00:00 PM
Tuesday, 16 July 2019 4:00:00 PM  
Monday, 15 July 2019 4:00:00 PM