【问题标题】:Powershell access property stored in JSON object存储在 JSON 对象中的 Powershell 访问属性
【发布时间】:2020-11-10 04:04:04
【问题描述】:
$primaryEndpoints = az storage account show --resource-group rg --name sto --query 'primaryEndpoints'

以上命令返回

{
  "blob": "https://sto.blob.core.windows.net/",
  "dfs": null,
  "file": "https://sto.file.core.windows.net/",
  "internetEndpoints": null,
  "microsoftEndpoints": null,
  "queue": "https://sto.queue.core.windows.net/",
  "table": "https://sto.table.core.windows.net/",
  "web": null
}

但是他下面的命令什么也没返回:

echo $primaryEndpoints["blob"]

我也试过

echo $primaryEndpoints.blob

如何访问 json 属性?

【问题讨论】:

    标签: json powershell powershell-4.0


    【解决方案1】:

    在我看来,您将 JSON 字符串作为返回值。要按名称访问属性,您需要先将 JSON 字符串转换为 PSObject。

    $primaryEndpoints = az storage account show --resource-group rg --name sto --query 'primaryEndpoints'
    
    $primaryEndpointObjects = $primaryEndpoints | ConvertFrom-Json
    
    $primaryEndpointObjects.blob
    

    【讨论】:

    • 没问题。很高兴能提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    相关资源
    最近更新 更多