use*_*778 19 powershell json response
将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
Run Code Online (Sandbox Code Playgroud)
我得到了以下回应:
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
Run Code Online (Sandbox Code Playgroud)
内容包含一些有线数字,所以我去了RawContent,我如何解析里面的JSON,忽略标题?或者有一种干净的方式从这些数字中获取内容?
Fro*_* F. 51
您可以将json响应自动转换为a 替换Invoke-WebRequest,以便您可以使用:Invoke-RestMethodpsobject
$response = Invoke-RestMethod -Uri "https://yadayada:8080/bla"
$response.flag
Run Code Online (Sandbox Code Playgroud)
小智 14
如果您需要使用Invoke-WebRequestoverInvoke-RestMethod您可以先将其转换为字符串来将其转换为对象
$response = Invoke-WebRequest -Uri "https://yadayada:8080/bla"
$jsonObj = ConvertFrom-Json $([String]::new($response.Content))
Run Code Online (Sandbox Code Playgroud)
小智 7
这边走:
$response = Invoke-WebRequest -Uri <your_uri>
if ($response.statuscode -eq '200') {
$keyValue= ConvertFrom-Json $response.Content | Select-Object -expand "<your_key_name>"
}
Run Code Online (Sandbox Code Playgroud)