【问题标题】:Parse value from JSON when key contains dot (period) in Powershell当键在 Powershell 中包含点(句点)时从 JSON 解析值
【发布时间】:2019-03-18 03:17:06
【问题描述】:

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

使用

$response = Invoke-RestMethod -Url $someUri -Method Get -ContentType "application/json" -Headers $headers
$wit = response.fields.System.WorkItemType

由于点/句点搞砸了,所以不起作用。

在 javascript 中有这个 question and answer 但在 Powershell 中不起作用。

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

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

【问题讨论】:

  • 您可以使用引号,以便按字面意思解析,试试$wit = response.fields.'System.WorkItemType'$wit = response.fields."System.WorkItemType"(两者都应该工作)。
  • 成功了!非常感谢!

标签: json powershell jsonpath


【解决方案1】:

如果你把属性名放在""'',你应该可以得到你要找的东西:

$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'

如果您需要转义双引号,请使用 ` grave accent,在 PowerShell 中称为 反引号

"area path = $($j.fields.`"System.AreaPath`")"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 2011-02-19
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    相关资源
    最近更新 更多