[英]Not that kind of Map exception with Jenkins and Groovy
I have a string in groovy that I want to convert into a map.我有一个 groovy 字符串,我想将其转换为地图。 When I run the code on my local computer through a groovy script for testing, I have no issues and a lazy map is returned.
当我通过 groovy 脚本在本地计算机上运行代码进行测试时,我没有任何问题并且返回了一个惰性映射。 I can then convert that to a regular map and life goes on.
然后我可以将其转换为常规地图,然后生活就会继续。 When I try the same code through my Jenkins DSL pipeline, I run into the exception
当我通过我的 Jenkins DSL 管道尝试相同的代码时,我遇到了异常
groovy.json.internal.Exceptions$JsonInternalException: Not that kind of map
Here is the code chunk in question:这是有问题的代码块:
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)
returned value:返回值:
[value2:[red gold:false], value1:[red pink:true, gold silver:true, blue green:true], value3:[silver brown:false]]
I know that Jenkins and Groovy differ in various ways, but from searches online others suggest that I should be able to use the LAX JsonSlurper library within my groovy pipeline.我知道 Jenkins 和 Groovy 在各种方面有所不同,但从网上搜索其他人建议我应该能够在我的 groovy 管道中使用 LAX JsonSlurper 库。 I am trying to avoid hand rolling my own string to map converter and would prefer to use a library if it's out there.
我试图避免手动滚动我自己的字符串到映射转换器,并且如果它在那里,我更喜欢使用库。 What could be the difference here that would cause this behavior?
这里有什么不同会导致这种行为?
Try to use尝试使用
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))
You can find information about normalize
here .您可以在此处找到有关
normalize
信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.