从 JSON 中提取值

Extract value from JSON

我正在尝试使用 PowerShell 从 JSON 对象中提取值,我有以下 JSON:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
     "clusterName": {
      "value": "hbasehd35"
    },
     "location": {
      "value": "East US 2"
    },
     "clusterType": {
      "value": "hbase"
    },
     "clusterVersion": {
      "value": "3.5"
    },
     "clusterWorkerNodeCount": {
      "value": 5
    },
     "subnetName": {
      "value": "hbase-subnet"
    },
     "headNodeSize": {
      "value": "Standard_D12_v2"
    },
     "workerNodeSize": {
      "value": "Standard_D12_v2"
    },
     "zookeeperSize": {
      "value": "Large"
    },
     "clusterStorageAccountName": {
      "value": "hbasestorage"
    },
     "storageAccountType": {
      "value": "Standard_GRS"
    },
     "Environment": {
      "value": "test"
    }
  }
}

在这里,我想使用 powershell 从这个文件中提取 clusterStorageAccountName,并将其分配给变量。

有人知道怎么做吗?

使用 Get-Content cmdlet 读取文件,使用 ConvertFrom-Json cmdlet 转换它,然后只访问您想要的 属性:

$yourVariable = (Get-Content 'yourJsonFilePath.json' | ConvertFrom-Json).parameters.clusterStorageAccountName.value

我不知道为什么,但是上面消息中的方法对我不起作用。但是可以在 "ConvertFrom-Json" 命令后通过按键获取所需的密钥,例如:

$yourVariable = (Get-Content 'yourJsonFilePath.json' | ConvertFrom-Json).clusterStorageAccountName