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

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

问题描述

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

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

最初,我受到 this 的启发并尝试:

Originally I was inspired by this and tried:

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

但这失败了:

无法为WriteJSONStep实例化{file = message1.json,json = {a = test}}(文件:String,json:JSON {},漂亮?:int):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天全站免登陆