任何建议如何从此JSON文件提取结果以Powershell下面提到的结果
JSON文件
{
"meta": {
"effective_params": {
"data_set": "Website",
"base_currencies": [
"USD"
]
},
"endpoint": "spot",
"request_time": "2018-06-20T18:06:59+00:00",
"skipped_currency_pairs": []
,
“引号”:[
{
"base_currency": "USD",
"quote_currency": "AUD",
"bid": "1.35494",
"ask": "1.35516",
"midpoint": "1.35505"
},
{
"base_currency": "USD",
"quote_currency": "BRL",
"bid": "3.75455",
"ask": "3.75705",
"midpoint": "3.75580"
]
}
我尝试获取此结果:
request_time :2018-06-20T18:06:59+00:00
base_currency : USD
quote_currency : ZAR
bid : 13.6187
ask : 13.6276
midpoint : 13.6232
答案 0 :(得分:0)
首先,您的JSON对象无效。您在第二个中点之后缺少}。
$json = Get-content -raw -path C:\path\to\file.json
$obj = ConvertFrom-Json -InputObject $json
##get all quote info
$obj.quotes
##get one set
write-host request_time: $obj.meta.request_time
write-host base_currency: $obj.quotes[0].base_currency
write-host quote_currency: $obj.quotes[0].quote_currency
write-host bid: $obj.quotes[0].bid
write-host ask: $obj.quotes[0].ask
write-host midpoint: $obj.quotes[0].ask
##loop through
$rTime = $obj.meta.request_time;
ForEach($i in $obj.quotes){
write-host request_time: $rTime
write-host base_currency: $i.base_currency
write-host quote_currency: $i.quote_currency
write-host bid: $i.bid
write-host ask: $i.ask
write-host midpoint: $i.ask
""
}
答案 1 :(得分:0)
这应该可以解决问题:
$json = @"
{
"meta": {
"effective_params": {
"data_set": "Website",
"base_currencies": [
"USD"
]
},
"endpoint": "spot",
"request_time": "2018-06-20T18:06:59+00:00",
"skipped_currency_pairs": []
},
"quotes":[
{
"base_currency": "USD",
"quote_currency": "AUD",
"bid": "1.35494",
"ask": "1.35516",
"midpoint": "1.35505"
},
{
"base_currency": "USD",
"quote_currency": "BRL",
"bid": "3.75455",
"ask": "3.75705",
"midpoint": "3.75580"
}
]
}
"@
$obj = $json | ConvertFrom-Json
$result = foreach( $quote in $obj.quotes ) {
"request_time : $($obj.meta.request_time)"
"base_currency : $($quote.base_currency)"
"quote_currency : $($quote.quote_currency)"
"bid : $($quote.bid)"
"ask : $($quote.ask)"
"midpoint : $($quote.midpoint)"
}
$result
答案 2 :(得分:0)
假设这是来自其他一些检索JSON对象的PowerShell代码,并且(再次)假设您只是尝试在屏幕上显示JSON,这真的很简单。
包装用于检索JSON结果的代码,通过管道传递到ConvertFrom-Json
,然后访问quotes
成员集合。
例如:
(('<code that returns a JSON object>') | ConvertFrom-Json).quotes
更现实一点...
((Invoke-WebRequest -Uri "HTTPS://Website.domain/API?param=value").Content | ConvertFrom-Json).quotes
或者如果您简单地想在PowerShell中导入原始JSON并以本地PowerShell格式查看报价对象:
((Get-content -Path C:\path\to\file.json) | ConvertFrom-Json).quotes
或者存储JSON对象并在以后使用:
$JSON = (Get-content -Path C:\path\to\file.json) | ConvertFrom-Json
$JSON.quotes