使用Newtonsoft Json和Powershell解析Json对象

时间:2013-07-22 21:12:37

标签: json powershell powershell-v3.0 json.net

我接受一个json对象作为命令行输入&然后尝试在Powershell 3.0中解析它。

代码如下 -

# Accept the command line json parameter
param(
 [string]$json = $(throw '-json parameter is required')
)

$currentPath = Get-Location

# Load the Dot Net Json Library
[Reflection.Assembly]::LoadFile("$currentPath\Newtonsoft.Json.dll")

# Parsed result stored
$result = [Newtonsoft.Json.Linq.JObject]::Parse($json)

write-host "$result"

foreach ($singleResult in $result)
{    
    foreach ($units in $singleResult) 
    { 
      foreach ($unit in $units)
      {
         write-host "$unit" -foreground DarkYellow

         $TechnologyName = $unit.TechnologyName.ToString();
         write-host "$TechnologyName"


         $OutputValue = $unit.OutputValue.ToString();
         write-host "$OutputValue"               
      }  
    }     
}

命令行如下 -

PS C:\Users\aghosh\Desktop> .\test.ps1 -json '{"DevResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"}],"QaResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"}],"PreProdResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"}],"ProdResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"},{"TechnologyName":"TFS","RuleName":"Process Template","OutputValue":"Raymond James CMMI V3.5"}]}'

现在我想进入部分 -

"DevResults": [
  {
    "TechnologyName": "TFS",
    "RuleName": "Alt CI ID for ESB",
    "OutputValue": "ESClientCenter"
  },
  {
    "TechnologyName": "TFS",
    "RuleName": "TFS Team Project Name",
    "OutputValue": "ClientCenter"
  }

你能给我合适的指示。谢谢。

0 个答案:

没有答案