1

我可以通过 Invoke-RestMethod 从提供以下格式的 API 取回数据:

    1612142668000000000 : @{peak_performance=29.18; current_utilization=19.15}
    1612146268000000000 : @{peak_performance=29.05; current_utilization=20.03}
    1612149868000000000 : @{peak_performance=29.07; current_utilization=18.86}

如果有帮助,上面的 JSON 格式是:

{
  "1612142668000000000": {
    "peak_performance": 29.18,
    "current_utilization": 19.15
  },
  "1612146268000000000": {
    "peak_performance": 29.05,
    "current_utilization": 20.03
  },
  "1612149868000000000": {
    "peak_performance": 29.07,
    "current_utilization": 18.86
  }
}

我希望能够计算和显示所有可用值的平均值peak-performancecurrent_utilization但我不知道如何做到这一点。有任何想法吗?

4

1 回答 1

0

有很多解决方案:

$json = @"
{
  "1612142668000000000": {
    "peak_performance": 29.18,
    "current_utilization": 19.15
  },
  "1612146268000000000": {
    "peak_performance": 29.05,
    "current_utilization": 20.03
  },
  "1612149868000000000": {
    "peak_performance": 29.07,
    "current_utilization": 18.86
  }
}
"@

$jout = $json | ConvertFrom-Json
$result = $jout.PSObject.Properties.Value | Measure-Object -Property current_utilization,peak_performance -Average

foreach($v in $result){
    Write-Host $v.Property   " = "  $v.Average
}

结果:

current_utilization  =  19.3466666666667
peak_performance  =  29.1
于 2021-05-21T11:00:22.577 回答