尝试Azure中的一些新功能; Azure函数运行Powershell。有一件事我挣扎了一段时间:我无法以普通的JSON获得输出,而是看似格式化并包含转义字符并且混乱了双引号。
function.json:
{
"bindings": [
{
"name": "req",
"type": "httpTrigger",
"direction": "in",
"webHookType": "genericJson",
"methods": [
"post",
"head",
"trace"
]
},
{
"name": "res",
"type": "http",
"direction": "out"
}
],
"disabled": false
}
run.ps1:
$ApiURI="http://avwx.rest/api/metar.php?station=ymml&format=JSON"
$Result = Invoke-RestMethod -Uri $ApiURI
Out-File -Encoding Ascii -FilePath $res -inputObject $Result
以下回复的结果:
"\r\n\r\nAltimeter : 1015\r\nCloud-List : {FEW 035}\r\nDewpoint : 11\r\nFlight-Rules : VFR\r\nOther-List : {}\r\nRaw-Report : YMML 210900Z 17010KT 9999 FEW035 19/11 Q1015 NOSIG\r\nRemarks : NOSIG\r\nRemarks-Info : \r\nRunway-Vis-List : {}\r\nStation : YMML\r\nTemperature : 19\r\nTime : 210900Z\r\nUnits : @{Altimeter=hPa; Altitude=ft; Temperature=C; Visibility=m; \r\n Wind-Speed=kt}\r\nVisibility : 9999\r\nWind-Direction : 170\r\nWind-Gust : \r\nWind-Speed : 10\r\nWind-Variable-Dir : {}\r\n\r\n\r\n\r\n"
但我期望的是:
{
"Altimeter": "1016",
"Cloud-List": [],
"Dewpoint": "12",
"Flight-Rules": "VFR",
"Other-List": [],
"Raw-Report": "YMML 210930Z 17011KT CAVOK 18/12 Q1016 NOSIG",
"Remarks": "NOSIG",
"Remarks-Info": {},
"Runway-Vis-List": [],
"Station": "YMML",
"Temperature": "18",
"Time": "210930Z",
"Units": {
"Altimeter": "hPa",
"Altitude": "ft",
"Temperature": "C",
"Visibility": "m",
"Wind-Speed": "kt"
},
"Visibility": "9999",
"Wind-Direction": "170",
"Wind-Gust": "",
"Wind-Speed": "11",
"Wind-Variable-Dir": []
}
这里可能有什么问题?
答案 0 :(得分:2)
我不确定你是如何得到这个结果的,因为Invoke-RestMethod
将数据存储在ResponseData
属性中。
当您使用Invoke-RestMethod
时,它会将json数据转换为Powershell对象。在run.ps1中,您似乎要将PS对象保存到文件中,而不是JSON数据。
如果您不打算修改JSON数据,将它转换为PSObject真的没用。所以你可以改用Invoke-webrequest
。
但是如果你想修改它。
$uri = "http://avwx.rest/api/metar.php?station=ymml&format=JSON"
$request = Invoke-RestMethod -Uri $uri
#Get the result and convert it back to JSON
$Result = $request.responseData | ConvertTo-Json
$Result | Out-File .\data.json -Encoding Ascii
答案 1 :(得分:0)
@David - 是的,以下适用于Azure功能。
一个。 run.ps1
文件内容
$ApiURI="http://avwx.rest/api/metar.php?station=ymml&format=JSON"
$Result = Invoke-RestMethod -Uri $ApiURI | ConvertTo-Json
Out-File -Encoding Ascii -FilePath $res -inputObject $Result
湾在Azure Functions Portal和Postman中请求结果。
{
"Altimeter": "1016",
"Cloud-List": [
[
"FEW",
"016"
]
],
"Dewpoint": "13",
"Flight-Rules": "VFR",
"Other-List": [],
"Raw-Report": "YMML 212100Z 28005KT 9999 FEW016 15/13 Q1016 NOSIG",
"Remarks": "NOSIG",
"Remarks-Info": {},
"Runway-Vis-List": [],
"Station": "YMML",
"Temperature": "15",
"Time": "212100Z",
"Units": {
"Altimeter": "hPa",
"Altitude": "ft",
"Temperature": "C",
"Visibility": "m",
"Wind-Speed": "kt"
},
"Visibility": "9999",
"Wind-Direction": "280",
"Wind-Gust": "",
"Wind-Speed": "05",
"Wind-Variable-Dir": []
}