Jenkins Groovy解析json

时间:2017-03-02 08:56:31

标签: json jenkins groovy jenkins-pipeline

我得到了以下JSON:

{
  "dev": {
    "hoster": "123",
    "id": "123",
  },
  "stage": {
    "hoster": "123",
    "id": "123",
    "merge": "dev",
    "slackChannel": "#dg-test-deployments"
  },
  "master": {
    "hoster": "123",
    "id": "123",
    "merge": "stage",
  },
  "updates": {
    "hoster": "123",
    "id": "123",
    "merge": "master",
    "slackChannel": "#dg-test-deployments"
  }
}

想要检查 dev stage master updates 是否存在。

有什么建议如何在groovy中做到这一点? :)

2 个答案:

答案 0 :(得分:6)

您可以尝试例如:

import groovy.json.JsonSlurper

def json = '''{
  "dev": {
    "hoster": "123",
    "id": "123",
  },
  "stage": {
    "hoster": "123",
    "id": "123",
    "merge": "dev",
    "slackChannel": "#dg-test-deployments"
  },
  "master": {
    "hoster": "123",
    "id": "123",
    "merge": "stage",
  },
  "updates": {
    "hoster": "123",
    "id": "123",
    "merge": "master",
    "slackChannel": "#dg-test-deployments"
  }
}'''
def slurped = new JsonSlurper().parseText(json)
assert slurped.keySet().containsAll(['dev', 'stage', 'master', 'updates'])

答案 1 :(得分:3)

渠道支持readJSONwriteJSON now