繁体   English   中英

Jenkins 和 Groovy 不是那种 Map 异常

[英]Not that kind of Map exception with Jenkins and Groovy

我有一个 groovy 字符串,我想将其转换为地图。 当我通过 groovy 脚本在本地计算机上运行代码进行测试时,我没有任何问题并且返回了一个惰性映射。 然后我可以将其转换为常规地图,然后生活就会继续。 当我通过我的 Jenkins DSL 管道尝试相同的代码时,我遇到了异常

groovy.json.internal.Exceptions$JsonInternalException: Not that kind of map

这是有问题的代码块:

  import groovy.json.*

  String string1 = "{value1={blue green=true, red pink=true, gold silver=true}, value2={red gold=false}, value3={silver brown=false}}"

  def stringToMapConverter(String stringToBeConverted){
      formattedString = stringToBeConverted.replace("=", ":")
      def jsonSlurper = new JsonSlurper().setType(JsonParserType.LAX)
      def mapOfString = jsonSlurper.parseText(formattedString)
      return mapOfString
  }

  def returnedValue = stringToMapConverter(string1)

  println(returnedValue)

返回值:

[value2:[red gold:false], value1:[red pink:true, gold silver:true, blue green:true], value3:[silver brown:false]]

我知道 Jenkins 和 Groovy 在各种方面有所不同,但从网上搜索其他人建议我应该能够在我的 groovy 管道中使用 LAX JsonSlurper 库。 我试图避免手动滚动我自己的字符串到映射转换器,并且如果它在那里,我更喜欢使用库。 这里有什么不同会导致这种行为?

尝试使用

import groovy.json.*

//@NonCPS
def parseJson(jsonString) {
    // Would like to use readJSON step, but it requires a context, even for parsing just text.
    def lazyMap = new JsonSlurper().setType(JsonParserType.LAX).parseText(jsonString.replace("=", ":").normalize())

    // JsonSlurper returns a non-serializable LazyMap, so copy it into a regular map before returning
    def m = [:]
    m.putAll(lazyMap)
    return m
}

String string1 = "{value1={blue green=true, red pink=true, gold silver=true}, value2={red gold=false}, value3={silver brown=false}}"
def returnedValue = parseJson(string1)
println(returnedValue)
println(JsonOutput.toJson(returnedValue))

您可以在此处找到有关normalize 信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM