本文介绍了在groovy中解析JSON以获取值(python dict)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典,作为groovy中的python字典,然后分配给一个变量x:

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

我想解析上面的内容并获取值:


  • JIRACHEF li>
  • JIRADEPLOYER

  • JIRASINGLEBUILD



最优雅的时髦您可以使用LAX slurper(在Groovy的最新版本中):

$ b($ {

>解决方案


$ b

  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


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 ?

解决方案

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

这篇关于在groovy中解析JSON以获取值(python dict)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:12