在 Windows Power Shell 中读取 json 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23343077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:17:46  来源:igfitidea点击:

Reading json response in windows power shell

jsonpowershell

提问by Shashi Ranjan

I am using power shell code as:

我使用电源外壳代码为:

   $web_client = new-object system.net.webclient
    $build_info=web_client.DownloadString("http://<URL>")
    $suitevm_build_number=
    $suitevmCLN=
    $webapp_build=
    $stats_build=

Output in browser while hitting http:// is:

点击 http:// 时浏览器中的输出是:

{"message":null,"changeset":"340718","branch":"main","product":"productname","buildNumber":"1775951","todaysDate":"28-4-2014"}

What should I write the power shell code to get:

我应该写什么power shell代码来得到:

  $suitevm_build_number=
    $suitevmCLN=
    $webapp_build=
    $stats_build=

回答by Frode F.

You're question is very unclear. If you have Powershell 3 or later, you can use ConvertFrom-JSONto convert the JSON-response to a object.

你的问题很不清楚。如果您有 Powershell 3 或更高版本,则可以使用ConvertFrom-JSON将 JSON 响应转换为对象。

$build_info=$web_client.DownloadString("http://<URL>") | ConvertFrom-Json

Ex of output:

输出示例:

$build_info

message     : 
changeset   : 340718
branch      : main
product     : productname
buildNumber : 1775951
todaysDate  : 28-4-2014

With PS 3+ you could also replace the webclient with Invoke-RestMethodas shown by @RickH.

使用 PS 3+,您还可以用 @RickHInvoke-RestMethod所示替换webclient。

$build_info = Invoke-RestMethod -Uri "http://<URL>"