How to parse JSON from the Invoke-WebRequest in PowerShell?

前端 未结 3 492
死守一世寂寞
死守一世寂寞 2020-12-03 02:53

When sending the GET request to the server, which uses self-signed certificate:

add-type @\"
    using System.Net;
    using System.Security.Cryptography.X50         


        
相关标签:
3条回答
  • 2020-12-03 03:22

    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 
    
    0 讨论(0)
  • 2020-12-03 03:26

    If you have a need to use Invoke-WebRequest over Invoke-RestMethod you can convert it to an object by turning it into a string first

    $response = Invoke-WebRequest -Uri "https://yadayada:8080/bla"
    $jsonObj = ConvertFrom-Json $([String]::new($response.Content))
    
    0 讨论(0)
  • 2020-12-03 03:34

    This way:

    $response = Invoke-WebRequest -Uri <your_uri>
    if ($response.statuscode -eq '200') {
        $keyValue= ConvertFrom-Json $response.Content | Select-Object -expand "<your_key_name>"
    }
    
    0 讨论(0)
提交回复
热议问题