jenkins - Looping in a C-style For loop instead of using each -


how can loop through json file using for loop in groovy? able .each in situation/bug cannot use .each loops.

the json file being read , parsed object.

the json looks this:

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

note: can modify json file, if need make process simpler.. try loop throgh , extract values keys.

so given json in string so:

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}}"         }     } }''' 

you can do:

import groovy.json.*  def json = new jsonslurper().parsetext(jsontext)  for(entry in json) {     println "$entry.key has file $entry.value.file" } 

to print:

workflows1 has file release1/wf_multifolder.xml workflows2 has file release1/wf_multifolder.xml 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -