从JSON中获取价值

问题描述 投票:5回答:2

我试图使用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,并将其分配给变量。

有人知道怎么做吗 ?

json powershell powershell-v4.0
2个回答
7
投票

使用Get-Content cmdlet来读取文件,使用ConvertFrom-Json cmdlet的转换它,只是访问所需的属性:

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

0
投票

我不知道为什么,但是从消息的方法上面没有为我工作。但工程刚开始键,你需要通过后“ConvertFrom JSON的”命令,e.g关键:

$yourVariable = (Get-Content 'yourJsonFilePath.json' | ConvertFrom-Json).clusterStorageAccountName
© www.soinside.com 2019 - 2024. All rights reserved.