使用groovy仅从JSON对象获取键

时间:2016-03-18 10:02:15

标签: json groovy

这是我的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仅获取对象键名称?

1 个答案:

答案 0 :(得分:6)

您可以使用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() 
相关问题