如何使用 Windows 命令行将 JSON 文件解析为变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19356220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse a JSON file to variables with the windows command line?
提问by Kriem
I have this JSONfile called test.jsonwhich says:
我有一个JSON名为的文件test.json,它说:
{
"test":true,
"limit":
{
"min":0.5,
"max":1.5
}
}
I'd like to be able to read this file in the Windows command line and have these objects parsed as variables.
我希望能够在 Windows 命令行中读取此文件并将这些对象解析为变量。
How would I do this?
我该怎么做?
采纳答案by Idan Arye
If you use Windows Powershell as your command line, you can use the ConvertFrom-JSONcmdlet: http://powershelljson.codeplex.com/
如果您使用 Windows Powershell 作为命令行,则可以使用ConvertFrom-JSONcmdlet:http: //powershelljson.codeplex.com/
Make sure your PowerShell version is above 3.0, as ConvertFrom-JSON is available from that version.
确保您的 PowerShell 版本高于 3.0,因为ConvertFrom-JSON 可从该版本获得。
If you use plain old CMD, you'll need an external tool for it. I like jq: http://stedolan.github.io/jq/. The tutorial uses curlfor the examples, but you can just as easily use echoor read the JSON from a file.
如果您使用普通的旧 CMD,则需要一个外部工具。我喜欢 jq:http://stedolan.github.io/jq/ 。本教程curl用于示例,但您可以同样轻松地使用echo或读取文件中的 JSON。
回答by Григорий
PowerShell example.
Create from file: $js = Get-Content file.json | ConvertFrom-Json.Get subkey: $js.key.subkey
PowerShell 示例。从文件创建:$js = Get-Content file.json | ConvertFrom-Json.获取子项:$js.key.subkey

