gpt4 book ai didi

jenkins - 如何解析数据,并写入 JSON 文件

转载 作者:行者123 更新时间:2023-12-05 06:27:30 26 4
gpt4 key购买 nike

在这里,我们将 JSON 内容中的值“dpidsha1”从 1234 替换为另一个值“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.find{it.dpidsha1}.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

错误:

[Pipeline] End of Pipeline an exception which occurred: in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals

Caused: java.io.NotSerializableException: groovy.json.JsonBuilder

我该如何解决这个问题?

最佳答案

我更愿意使用 Utility Jenkins Pipelines 的函数而不是 groovy 类。下面的管道工作。我最后简化了转储到屏幕以验证使用 bash cat 命令的结果。但结果就是我想的那样。

node('linux') {
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 = readJSON text: content
println (slurped)
def builder = slurped
builder.device.find{it.dpidsha1 == "1234"}.dpidsha1= "abcd"
println(builder)

writeJSON file: 'uselessfile.json', json: builder, pretty: 4
sh 'cat uselessfile.json'
}

关于jenkins - 如何解析数据,并写入 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55233497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com