这是我的 JSON 对象
{
"master": {
"node": "xyz",
"files": [{"type": "modified", "file": "test.txt"}]
},
"testbranch2": {
"node": "abc",
"files": [{"type": "modified", "file": "test.txt"}]
},
"testbranch": {
"node": "xxx",
"files": [{"type": "modified", "file": "test.txt"}],
}
}
我只需要对象键名称,例如“master”、“testbranch2”、“testbranch”。如何使用 groovy 仅获取对象键名称?
最佳答案
您可以使用 JsonSlurper
import groovy.json.JsonSlurper
def json = '{ "master": ...'
def test = new JsonSlurper().parseText(json)
//if json comes from file you can do: new JsonSlurper().parse(new File('YOUR_JSON_FILE'))
println test.keySet()
关于json - 使用 groovy 仅从 JSON 对象中获取键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36081406/