PowerShell:使用原生的更短命令处理JSON?

jaxagkaj  于 2023-11-20  发布在  Shell
关注(0)|答案(1)|浏览(96)

我通常使用JQ来处理JSON。

PS C:\> gc pw.txt
{
    "PowerShell": {
        "is a task": {
            "automation": [
                {
                    "and": "configuration",
                    "management": [
                        {
                            "program": "from",
                            "microsoft": {
                                "consisting of": "a command-line",
                                "shell": {
                                    "and": "the",
                                    "associated": {
                                        "scripting": {
                                            "language.": "initially a windows",
                                            "component": {
                                                "only": "known as",
                                                "windows": {
                                                    "powershell": "7.3.9-win-x64.msi",
                                                    "it was made": "open-source"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    ]
                },
                {
                    "the former": "is built on the .NET Framework",
                    "the latter on": ".NET (previously .NET Core)."
                }
            ]
        }
    }
}

字符串
下面的方法在PowerShell中可以很好地从上面的JSON中获取值7.3.9-win-x64.msi

PS C:\> gc pw.txt | jq -r '.[] | .[] | .[] | .[].management | .[0].microsoft | .shell.associated.scripting.component.windows.powershell'


那么,现在我如何获得相同的值,而不使用JQ,而是使用PowerShell本身的内置实用程序(尽可能使用更短的命令)?

uelo1irk

uelo1irk1#

$jsonObject=gc pw.txt | ConvertFrom-JSON
$jsonObject.powershell."is a task".automation[0].management[0].microsoft.shell.associated.scripting.component.windows.powershell

字符串

相关问题