如何在PowerShell中从Invoke-WebRequest解析JSON? [英] How to parse JSON from the Invoke-WebRequest in PowerShell?

查看:711
本文介绍了如何在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

内容包含一些有线号码,因此我追求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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆