parse JSON in groovy to get values (python dict)

我们两清 提交于 2019-12-25 16:47:12

问题


I have a dictionary that I get as a python dictionary in groovy which I then assign to a variable x :

def x = "{'JIRACHEF': 'PIBEP-2135', 'JIRADEPLOYER': 'PIBEP-2136', 'JIRASINGLEBUILD': 'PIBEP-2137'}"

I want to parse the above and get values for :

  • JIRACHEF
  • JIRADEPLOYER
  • JIRASINGLEBUILD

whats the most elegant groovy way of doing it ?


回答1:


You can use the LAX slurper (in recent versions of Groovy):

import groovy.json.*

def x = "{'JIRACHEF': 'PIBEP-2135', 'JIRADEPLOYER': 'PIBEP-2136', 'JIRASINGLEBUILD': 'PIBEP-2137'}"

def parsed = new JsonSlurper().setType(JsonParserType.LAX).parseText(x)

println parsed.JIRACHEF
println parsed.JIRADEPLOYER
println parsed.JIRASINGLEBUILD


来源:https://stackoverflow.com/questions/41680219/parse-json-in-groovy-to-get-values-python-dict

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