我接受一个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"
}
你能给我合适的指示。谢谢。