使用Groovy仅从JSON对象中获取键 [英] Fetch only keys from JSON Object using groovy
本文介绍了使用Groovy仅从JSON对象中获取键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的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仅获取对象键名?
I need only the object key names, like "master", "testbranch2","testbranch. How do I fetch only the object key names using groovy?
推荐答案
您可以使用JsonSlurper
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()
这篇关于使用Groovy仅从JSON对象中获取键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文