【问题标题】:Parsing json while executing a curl在执行 curl 时解析 json
【发布时间】:2014-02-18 02:37:14
【问题描述】:

我在我的 bash 脚本中执行以下 curl 请求以获取我的构建的覆盖率报告:

codeCoverage=$(curl -s $url/job/$jobname/$lastBuildNo/cobertura/api/json?depth=2)

这会返回一个如下所示的 json 响应:

{
"results": {
    "children": [
        {
            "children": [
                {}
            ],
            "elements": [
                {},
                {}
            ],
            "name": "com.test.web"
        },
        {
            "children": [
                {}
            ],
            "elements": [
                {},
                {}
            ],
            "name": "com.test.web.filters"
        }
    ],
    "elements": [
        {
            "name": "Classes",
            "ratio": 37.75
        },
        {
            "name": "Conditionals",
            "ratio": 11.63969
        },
        {
            "name": "Files",
            "ratio": 42.70073
        },
        {
            "name": "Lines",
            "ratio": 16.937233
        },
        {
            "name": "Methods",
            "ratio": 23.67906
        },
        {
            "name": "Packages",
            "ratio": 50
        }
    ],
    "name": "Cobertura Coverage Report"
}

}

我只是感兴趣

"name": "Lines",
"ratio": 16.937233

我可以在获取文本并提取该值后对其进行解析,但是当我发出 curl 请求时,有什么方法可以获取该值?

【问题讨论】:

  • 你的意思是输出将是:Lines, 16.937233 ?
  • 理想情况下我只想要 16.937233
  • 您的 json 文件在第 9 行无效。
  • 刚刚更正了...谢谢
  • 对此类任务使用python...

标签: json bash parsing curl


【解决方案1】:

请使用精彩的json utiljq:

jq -r '.results.elements[] | select(.name=="Lines").ratio' input.json

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    相关资源
    最近更新 更多