如何解析多级JSON数组?

How to parse multi-level JSON array?

这是我的Data.json。它具有多级阵列。我必须获取所有数组元素:

{
  "host": "http://localhost:5000",
  "dlls": [
    {
      "files": [
        {
          "path": ".\Xml.dll",
          "store": ".\DX\OpenXml.dll"
        },
        {
          "path": ".\Xml.dll",
          "store": ".\DX\OpenXml.dll"
        },
        {
          "path": ".\Xml.dll",
          "store": ".\DX\OpenXml.dll"
        },
        {
          "path": ".\Xml.dll",
          "store": ".\DX\OpenXml.dll"
        },
        {
          "path": ".\Xml.dll",
          "store": ".\DX\OpenXml.dll"
        }
      ]
    },
    {
      "json": [
        {
          "path": ".\index.json",
          "store": ".\DX\index.json"
        },
        {
          "path": ".\index.json",
          "store": ".\DX\index.json"        
        },
        {
          "path": ".\index.json",
          "store": ".\DX\index.json"        
        },
        {
          "path": ".\index.json",
          "store": ".\DX\index.json"        
        },
        {
          "path": ".\index.json",
          "store": ".\DX\index.json"        
        },
        {
          "path": ".\index.json",
          "store": ".\DX\index.json"        
        }
      ]
    }
  ]
}

在我当前的PS脚本中,我只能迭代一级:

$json = $null;
$jsonparsed = $null;
$validJson = $false;
try {
    $json = Get-Content -Raw $file; 
    $jsonparsed = ConvertFrom-Json $json -ErrorAction Stop;
    $validJson = $true;
} catch {
    $validJson = $false;
}

if ($validJson) {
    Write-Host "Provided text has been correctly parsed to JSON";
    Write-Host $jsonparsed;
} else {
    Write-Host "Provided text is not a valid JSON string" -ForegroundColor "Red";
    return;
}

我必须解析所有 JSON 数组元素。检索每个 "path" 和 "store" 的值。请告诉我如何在 PowerShell 版本 5 中执行此操作。我通过加载第三方程序集找到了解决方案。但我不允许使用任何外部组件。没有外部程序集是否可以解析?

如果将其粘贴到 PowerShell (v5) window:

$jsonparsed = convertFrom-Json @'
{
  "host": "http://localhost:5000",
  "dlls": [
    {
      "files": [
        {
          "path": ".\Xml.dll",
          "store": ".\DX\OpenXml.dll"
        },
        {
          "path": ".\Xml.dll",
          "store": ".\DX\OpenXml.dll"
        },
        {
          "path": ".\Xml.dll",
          "store": ".\DX\OpenXml.dll"
        },
        {
          "path": ".\Xml.dll",
          "store": ".\DX\OpenXml.dll"
        },
        {
          "path": ".\Xml.dll",
          "store": ".\DX\OpenXml.dll"
        }
      ]
    },
    {
      "json": [
        {
          "path": ".\index.json",
          "store": ".\DX\index.json"
        },
        {
          "path": ".\index.json",
          "store": ".\DX\index.json"        
        },
        {
          "path": ".\index.json",
          "store": ".\DX\index.json"        
        },
        {
          "path": ".\index.json",
          "store": ".\DX\index.json"        
        },
        {
          "path": ".\index.json",
          "store": ".\DX\index.json"        
        },
        {
          "path": ".\index.json",
          "store": ".\DX\index.json"        
        }
      ]
    }
  ]
}
'@

Write-Host $jsonparsed.host
ForEach ($dll in $jsonparsed.dlls) {
    ForEach ($file in $dll.files) {
        Write-Host $file.path
        Write-Host $file.store
    }
    ForEach ($json in $dll.json) {
        Write-Host $json.path
        Write-Host $json.store
    }
}

你应该得到这个:

.\Xml.dll
.\DX\OpenXml.dll
.\Xml.dll
.\DX\OpenXml.dll
.\Xml.dll
.\DX\OpenXml.dll
.\Xml.dll
.\DX\OpenXml.dll
.\Xml.dll
.\DX\OpenXml.dll
.\index.json
.\DX\index.json
.\index.json
.\DX\index.json
.\index.json
.\DX\index.json
.\index.json
.\DX\index.json
.\index.json
.\DX\index.json
.\index.json
.\DX\index.json