如何从 PowerShell 中的 Invoke-WebRequest 解析 JSON? [英] How to parse JSON from the Invoke-WebRequest in PowerShell?
本文介绍了如何从 PowerShell 中的 Invoke-WebRequest 解析 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
向使用自签名证书的服务器发送 GET 请求时:
When sending the GET request to the server, which uses self-signed certificate:
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 : 200
StatusDescription : 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
Content 包含一些有线数字,所以我去了 RawContent,我将如何解析里面的 JSON,忽略标头?或者有没有一种干净的方法可以从这些数字中获取内容?
Content contains some wired numbers, so I went after RawContent, how would I parse the JSON inside, ignoring headers? or is there a clean way to get Content from those numbers?
推荐答案
您可以将 Invoke-WebRequest
替换为 Invoke-RestMethod
,后者将 json 响应自动转换为 psobject
所以你可以使用:
You could replace Invoke-WebRequest
with Invoke-RestMethod
which auto-converts json response to a psobject
so you can use:
$response = Invoke-RestMethod -Uri "https://yadayada:8080/bla"
$response.flag
这篇关于如何从 PowerShell 中的 Invoke-WebRequest 解析 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文