如何在groovy中使用json file
循环遍历FOR
?我可以使用.each
来完成,但我处于一种情况/错误,我无法使用.each
循环。
正在读取json文件并将其解析为对象。
json看起来像这样:
{
"workflows1": {
"name": "/wf_multifolder",
"file": "release1/wf_multifolder.XML",
"folderNames": {
"multifolder": "{{multifolder}}",
"agent1": "{{agentx}}"
}
},
"workflows2": {
"name": "/wf_multifolder",
"file": "release1/wf_multifolder.XML",
"folderNames": {
"multifolder": "{{multifolder}}",
"agent1": "{{agentx}}"
}
}
}
注意:我可以修改json文件,如果需要使过程更简单..我尝试做的就是循环throgh并提取键的值。
答案 0 :(得分:1)
所以给出了像这样的字符串中的json:
def jsonText = '''{
"workflows1": {
"name": "/wf_multifolder",
"file": "release1/wf_multifolder.XML",
"folderNames": {
"multifolder": "{{multifolder}}",
"agent1": "{{agentx}}"
}
},
"workflows2": {
"name": "/wf_multifolder",
"file": "release1/wf_multifolder.XML",
"folderNames": {
"multifolder": "{{multifolder}}",
"agent1": "{{agentx}}"
}
}
}'''
你可以这样做:
import groovy.json.*
def json = new JsonSlurper().parseText(jsonText)
for(entry in json) {
println "$entry.key has file $entry.value.file"
}
打印:
workflows1 has file release1/wf_multifolder.XML
workflows2 has file release1/wf_multifolder.XML