在powershell中转换json时如何解决“无法绑定参数'uri'无法转换......”?

问题描述

我在 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 对象。查看下面的文档了解更多信息

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-7.1#description

$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