【发布时间】:2019-03-18 00:31:11
【问题描述】:
这里我们将 1234 中的“dpidsha1”值替换为 json 内容中的另一个值“abcd”,并且 我们正在尝试 将json格式的内容写入文件“uselessfile.json”,并打印文件“uselessfile.json”的内容
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def buildContent(){
def content = """
{
"app":{ },
"at":2,
"badv":[ ],
"bcat":[ ],
"device":[ {
"carrier":"310-410",
"connectiontype":3,
"devicetype":1,
"dnt":0,
"dpidmd5":"268d403db34e32c45869bb1401247af9",
"dpidsha1":"1234"
},
{
"carrier":"310-410",
"connectiontype":3,
"devicetype":1,
"dnt":0,
"dpidmd5":"268d403db34e32c45869bb1401247af9",
"dpidsha1":"1234"
}]
}"""
def slurped = new JsonSlurper().parseText(content)
def builder = new JsonBuilder(slurped)
builder.content.device.dpidsha1 = 'abcd'
println(builder.toPrettyString())
writeFile file: 'uselessfile.json', text: builder.toPrettyString(content)
File file = new File("uselessfile.json")
println "Below is the content of the file ${file.absolutePath}"
println uselessfile.json
错误:
[管道] 管道结束 发生的异常: 在字段 com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
原因:java.io.NotSerializableException:groovy.json.JsonBuilder
【问题讨论】:
-
jsonBuilder.toPrettyString(content)应该是builder.toPrettyString(content) -
将数据写入 uselessfile.json 文件时仍然失败。我已经更新了问题。
标签: jenkins groovy jenkins-pipeline jenkins-groovy