问题描述
我在 powershell 中做一个 Get-Weather
项目,我从 weatherapi.com 中提取数据。我能够使用 API 密钥成功连接到网站,但是,当我尝试在脚本中将其从 json 转换时,它不起作用。我得到的错误是:
“无法绑定参数 'Uri'。无法转换...” 我尝试了很多不同的方法来写这个:
$response = Invoke-RestMethod -uri $url -Method Get -ResponseHeadersVariable r -StatusCodeVariable s
$weatherobject = ConvertFrom-Json $url
对网站的请求是:
$url = Invoke-WebRequest "http://api.weatherapi.com/v1/forecast.json?key=$key&q=$location&days=$Days"
非常感谢您的帮助,谢谢!
解决方法
ConvertFrom-Json cmdlet 的输入是一个 JSON 对象。查看下面的文档了解更多信息
$url = "http://api.weatherapi.com/v1/forecast.json?key=$key&q=$location&days=$Days"
$response = Invoke-RestMethod -uri $url -Method Get -ResponseHeadersVariable r -StatusCodeVariable s
$weatherobject = ConvertFrom-Json $response