当键在 Powershell 中包含点(句点)时从 JSON 解析值
Parse value from JSON when key contains dot (period) in Powershell
在上面的示例中,我想从 "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' 值? (错误可能是其他字符串...)
如果你把 属性 个名字放在 ""
或 ''
中,你应该能得到你要找的东西:
$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 中称为 backtick。
"area path = $($j.fields.`"System.AreaPath`")"
在上面的示例中,我想从 "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' 值? (错误可能是其他字符串...)
如果你把 属性 个名字放在 ""
或 ''
中,你应该能得到你要找的东西:
$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 中称为 backtick。
"area path = $($j.fields.`"System.AreaPath`")"