如何在PowerShell中从Invoke-WebRequest解析JSON?
将GET请求发送到使用自签名证书的服务器时:
add-type @" using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$RESPONSE=Invoke-WebRequest -Uri https://yadayada:8080/bla -Method GET
echo $RESPONSE
我得到以下回应:
StatusCode : 200StatusDescription : OK
Content : {123, 10, 108, 111...}
RawContent : HTTP/1.1 200 OK
Content-Length: 21
Date: Sat, 11 Jun 2016 10:11:03 GMT
{
flag:false
}
Headers : {[Content-Length, 21], [Date, Sat, 11 Jun 2016 10:11:03 GMT]}
RawContentLength : 21
内容包含一些有线号码,因此我追随RawContent,该如何解析内部的JSON,而忽略标题?还是有一种干净的方法来从这些数字中获取内容?
回答:
你可以替换Invoke-WebRequest
使用Invoke-RestMethod
其自动转换JSON响应psobject
,以便您可以使用:
$response = Invoke-RestMethod -Uri "https://yadayada:8080/bla"$response.flag
以上是 如何在PowerShell中从Invoke-WebRequest解析JSON? 的全部内容, 来源链接: utcz.com/qa/400355.html