我试图通过API获得一些汇率,但我不知道如何将所有这些转换为一个表格,如:
date | timestamp | base | Currency Name | Currency |
我需要将费率扩展为两列货币名称和货币?
$request = website
$X = Invoke-WebRequest $request
$JSONResult = $x | ConvertFrom-Json
$JSONResult
{
"success": true,
"timestamp": 1519296206,
"base": "EUR",
"date": "2018-06-12",
"rates": {
"AUD": 1.566015,
"CAD": 1.560132,
"CHF": 1.154727,
"CNY": 7.827874,
"GBP": 0.882047,
"JPY": 132.360679,
"USD": 1.23396,
[...]
}
}
答案 0 :(得分:3)
转换后,JSON数据的rates
属性的值是一个自定义对象,其中货币名称为属性,汇率(?)为其值。使用该对象的内部属性PSObject
枚举对象属性及其值:
$JSONResult.rates.PSObject.Properties | ForEach-Object {
New-Object -Type PSObject -Property @{
'Date' = $JSONResult.date
...
'Currency Name' = $_.Name
'Currency' = $_.Value
}
}
答案 1 :(得分:0)
从输出:
> $JSONResult
success : True
timestamp : 1519296206
base : EUR
date : 2018-06-12
rates : @{AUD=1,566015; CAD=1,560132; CHF=1,154727; CNY=7,827874; GBP=0,882047; JPY=132,360679; USD=1,23396}
你可以推导出如何把它放在一起:
$request = 'http://data.fixer.io/api/latest?access_key=hexkeydeletedhexkeydeletedhexkey'
$x= Invoke-WebRequest $request
$JSONResult = $x | ConvertFrom-Json
If ($JSONResult.success){
$JSONResult.rates.PSObject.Properties | ForEach-Object {
New-Object -Type PSObject -Property @{
'date' = $JSONResult.date
'timestamp' = $JSONResult.timestamp
'base' = $JSONResult.base
'Currency Name' = $_.Name
'Currency' = $_.Value }
} | select date,timestamp,base,'Currency Name',Currency|ft -auto
}
示例输出:
date timestamp base Currency Name Currency
---- --------- ---- ------------- --------
2018-06-12 1519296206 EUR AUD 1,566015
2018-06-12 1519296206 EUR CAD 1,560132
2018-06-12 1519296206 EUR CHF 1,154727
2018-06-12 1519296206 EUR CNY 7,827874
2018-06-12 1519296206 EUR GBP 0,882047
2018-06-12 1519296206 EUR JPY 132,360679
2018-06-12 1519296206 EUR USD 1,23396
要将时间戳从unix日期时间格式转换为本地时间,您可以使用此功能
Function Convert-FromUnixDate ($UnixDate) {
[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($UnixDate))
}
> (Convert-FromUnixDate 1528900809):ToString('G')
2018-06-13 16:40:09