我得到了以下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中做到这一点? :)
答案 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)
渠道支持readJSON
和writeJSON
now。