PowerShell解析JSON文件中的键或值[重复]

toe95027  于 2023-07-01  发布在  Shell
关注(0)|答案(1)|浏览(184)

此问题已在此处有答案

PowerShell iterate through json of key value pairs(1个答案)
3小时前关闭
我有一个Json文件如下:

{
   "DevParametersList": {
      "PowerBIWorkspaceID": "062caXXXXXXXXXca3308a3b",
      "DatasetID": "f23a6b3d-d8fXXXXXXXa0a53c67"
   },
   "SITParametersValue": {
      "PowerBIWorkspaceID": "062cad8e-XXXXX8a3b",
      "DatasetID": "f23a6b3dXXXXa53c67"
   },
   "UATParametersValue": {
      "PowerBIWorkspaceID": "062cad8XXXX8a3b",
      "DatasetID": "f23a6b3d-d8XXXe5a0a53c67"
   },
   "ProdParametersValue": {
      "PowerBIWorkspaceID": "062cad8e-aXXX8a3b",
      "DatasetID": "f23a6b3XXXa8e5a0a53c67"
   }
}

现在,我想在标签页DevParametersList下逐个获取key和它的vlaue,这样我就可以循环它们了。
如何才能做到这一点?
我现在的Json文件是:

$WorkflowListPath = "$(FilePath)\ParametersChangeList.json"
$jsonObj = (Get-Content $WorkflowListPath | ConvertFrom-Json).DevParametersList
$test = $jsonObj.keys

Write-Host "The test keys is:$test

但是结果是null,因为$jsonObj的值应该是json文件的,但是我不知道继续做下去。

bd1hkmkf

bd1hkmkf1#

您可以使用隐藏的psobject成员集来枚举对象的属性:

$jsonObj.psobject.Properties |ForEach-Object {
    Write-Host "Found a property named '$($_.Name)' with value '$($_.Value)'"
}

相关问题