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

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

本文介绍了遍历JSON数组并打印出相同的键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Have JSON array with multiple keys of same which shows time in epoch, want to print then out and convert time to EST

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


推荐答案

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

You can try this out to print each objects converted Epoch to current timezone:

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

这篇关于遍历JSON数组并打印出相同的键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:25