Fetch only keys from JSON Object using groovy

扶醉桌前 提交于 2021-02-09 02:48:17

问题


This is my JSON Object

{
"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"}], 
   }
}

I need only the object key names, like "master", "testbranch2","testbranch. How do I fetch only the object key names using groovy?


回答1:


You can use 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() 


来源:https://stackoverflow.com/questions/36081406/fetch-only-keys-from-json-object-using-groovy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!