bash JSON 和 Cygwin - 如何解析、获取字段等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18360914/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:20:26  来源:igfitidea点击:

JSON and Cygwin - how to parse, get fields, etc

jsonbashapicommand-linecygwin

提问by Etay Liberman

I'm getting a JSON based output when sending GET requests to an API online, using Cygwin. I know how to manage JSON files over PHP and JS, but in this I wish to keep using Cygwin.

使用 Cygwin 在线向 API 发送 GET 请求时,我得到了基于 JSON 的输出。我知道如何通过 PHP 和 JS 管理 JSON 文件,但在这方面我希望继续使用 Cygwin。

Is there any way to "handle" those files, getting fields' value, etc? I know I can "create" something manually with sed, grep, awk and such - but I'm looking, first of all, for something which is "ready-to-use".

有没有办法“处理”这些文件,获取字段的值等?我知道我可以使用 sed、grep、awk 等手动“创建”一些东西——但我首先在寻找“随时可用”的东西。

Example:

例子:

{
    "campaign": {
        "name": "my campaign",
        "id": 1434,
        "creatives": [
                {
                    "id": 4162,
                    "state": "active"
                }
            ],
}
}

回答by dragon788

A great option is to use 'jq'. It is a command line JSON query tool. There is a source tarball available for Linux/Cygwin etc that you can build and use to query JSON directly as well as pipe it into other tools. https://stedolan.github.io/jq/download/

一个很好的选择是使用'jq'。它是一个命令行 JSON 查询工具。有一个可用于 Linux/Cygwin 等的源代码压缩包,您可以构建并使用它直接查询 JSON 以及将其通过管道传输到其他工具中。https://stedolan.github.io/jq/download/

回答by David Martinez Urrea

I think you can start from something like this:

我认为你可以从这样的事情开始:

curl HOST:PORT/API | jq -r '.KEY'