在Powershell中从数组中提取数据

时间:2018-04-01 18:34:28

标签: arrays powershell sorting

我使用以下内容填充数组:

forEach ($ip in $ipReturn.IPAddress) {
   try {
      $jsonObject = Invoke-RestMethod -Method Get -Uri "https://api.weatherchannel/forecast/daily?ip=$ip&days=2&units=I&key=$apiKey" -Headers $header 
      $jsonDataArray += $jsonObject
    }
    catch {
        $errorTrap = $error[0]
        Write-Host "Status Code:" $_.Exception 
        Write-Host "Status Description:" $_.Exception 
    }
}

我在PowerShell中的数组中填充了以下数据:

$jsonDataArray | foreach{Write-Host $_}
@{timezone=America/New_York; city_name=Azalea Park; lon=-81.3004; data=System.Object[]; state_code=FL; country_code=US; lat=28.5535}

我需要每个数组中的数据。特别是:data=System.Object

我曾考虑使用以下结构:

$foreach (system.object['data'] in $jsonDataArray)
{
    //pull and extract all data as string objects and populate those strings into a new array. Search each array string and pull data to create histogram.

}

我查看了代码。有谁知道如何实现这种数据提取?

1 个答案:

答案 0 :(得分:3)

不要让data字段的类型分散您的注意力:)

foreach($jsonData in $jsonDataArray)
{
    foreach($data in $jsonData.data)
    {
        # $data represents each object in the data array
    }
}