Jenkinsfile 管道构造 JSON 对象并写入文件 [英] Jenkinsfile pipeline construct JSON object and write to file

查看:24
本文介绍了Jenkinsfile 管道构造 JSON 对象并写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构造一个 JSON 对象并将内容写入文件.

I would like to construct a JSON object and write the content to file.

最初我受到这个的启发并尝试:

Originally I was inspired by this and tried:

def data = [
        a:"test: ${myVar}"
    ]
    writeJSON(file: 'message1.json', json: data)

但这失败了:

无法为 WriteJSONStep(file: String, json: JSON{}, pretty?: int) 实例化 {file=message1.json, json={a=test}}:java.lang.UnsupportedOperationException:必须指定 $class带有接口 net.sf.json.JSON 的实现

Could not instantiate {file=message1.json, json={a=test}} for WriteJSONStep(file: String, json: JSON{}, pretty?: int): java.lang.UnsupportedOperationException: must specify $class with an implementation of interface net.sf.json.JSON

所以接下来我尝试了:

def data = readJSON text: '{}'
data.a = "test: ${myVar}"
writeJSON(file: 'message1.json', json: data, pretty: 4)

现在构建通过了,但文件内容如下:

Now the build passes, but the content of the file looks like:

{
     "a":     {

        "bytes":         [

            114,

            101,

            108,

            101,

            97,

            115,

            101

            50

        ],

        "strings":         [

            "test: ",

            ""

        ],

        "valueCount": 1,

        "values": ["v1.0.2"]

    }
}

而我的意图是 {"a": "test: v1.0.2"}

我的最终目标是动态构造一个 JSON 对象,用一些动态数据设置一些属性,然后编写 JSON 文件.

My end goal is that I want to dynamically construct a JSON object, set some properties with some dynamic data and then write the JSON file.

是否有一些语法可用于将值分配为字符串,而不是一些字节.

Is there some syntax that can be used to assign the values as strings, rather than some bytes.

推荐答案

似乎解决方案之一是更改添加到地图的代码以指定 as String:

It seems one solution to this is changing the code that adds to the map to specify as String:

def data = readJSON text: '{}'
data.a = "test: ${myVar}" as String
writeJSON(file: 'message1.json', json: data, pretty: 4)

这篇关于Jenkinsfile 管道构造 JSON 对象并写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆