当密钥在Powershell中包含点(句点)时,从JSON中解析值

Pie*_*rre 2 powershell json jsonpath

在此输入图像描述

在上面的例子中,我想从"System.WorkItemType"中获取值"Bug"

运用

$response = Invoke-RestMethod -Url $someUri -Method Get -ContentType "application/json" -Headers $headers
$wit = response.fields.System.WorkItemType
Run Code Online (Sandbox Code Playgroud)

由于点/周期搞砸了,所以不起作用.

在javascript中有这个问题和答案,但在Powershell中不起作用.

使用正则表达式用下划线替换点似乎太牵强了(我没有让它工作,但使用-match -convertFrom-Json -replace -convertTo-Json所以可能做错了?

所以我的问题很简单:如何从'System.WorkItemType'键中获取'Bug'值?(bug可能是其他字符串...)

tho*_*her 7

如果您将属性名称放在""或中'',您应该能够得到您想要的内容:

$json= @'
 { "id" : 9983,
    "rev" : 17,
    "fields" :{
    "System.AreaPath":"Cloud\\Dev Blue Team",
    "System.TeamProject":"Cloud"
    }
}
'@

$j = $Json | convertfrom-json
$j.fields."System.AreaPath"
$J.fields.'System.TeamProject'
Run Code Online (Sandbox Code Playgroud)

如果你需要转义双引号,请使用` 严重重音,在PowerShell中称为反引号.

"area path = $($j.fields.`"System.AreaPath`")"
Run Code Online (Sandbox Code Playgroud)