构造JSON以与Jenkins的slackNotification插件一起使用

时间:2017-12-29 11:23:11

标签: arrays json jenkins groovy slack

鉴于以下JSON示例(来自Slack网站),我尝试了很多方法来构造该消息。我尝试了net.sf.json.JSONObjectnet.sf.json.JSONArray。但我无法正确完成它。

我将对象解释为:

1 net.sf.json.JSONObject,其中包含1个键值对'attachment'= array

1 net.sf.json.JSONArray.1,其中hashmap为字段

其中1个字段值为ref net.sf.json.JSONArray.2

包含1个元素的net.sf.json.JSONArray.2

此元素是另一个嵌套的net.sf.json.JSONObject

我做错了什么?我没有附上代码示例,因为我多次尝试并意识到我可能做错了。

{
"attachments": [
    {
        "fallback": "Required plain-text summary of the attachment.",
        "color": "#36a64f",
        "pretext": "Optional text that appears above the attachment block",
        "author_name": "Bobby Tables",
        "author_link": "http://flickr.com/bobby/",
        "author_icon": "http://flickr.com/icons/bobby.jpg",
        "title": "Slack API Documentation",
        "title_link": "https://api.slack.com/",
        "text": "Optional text that appears within the attachment",
        "fields": [
            {
                "title": "Priority",
                "value": "High",
                "short": false
            }
        ],
        "image_url": "http://my-website.com/path/to/image.jpg",
        "thumb_url": "http://example.com/path/to/thumb.png",
        "footer": "Slack API",
        "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
        "ts": 123456789
    }
]
}

2 个答案:

答案 0 :(得分:0)

您应该能够使用以下简单的脚本实现它:

def json = new groovy.json.JsonBuilder()
json {
    attachments ([ 
        {
            fallback 'Required plain-text summary of the attachment.'
            color '#36a64f'
            pretext 'Optional text that appears above the attachment block'
            author_name 'Bobby Tables'
            author_link 'http://flickr.com/bobby/'
            author_icon 'http://flickr.com/icons/bobby.jpg'
            title 'Slack API Documentation'
            title_link 'https://api.slack.com/'
            text 'Optional text that appears within the attachment'
            fields([
                {
                            title 'Priority'
                            value 'High'
                            'short' false
                }
            ])
            image_url 'http://my-website.com/path/to/image.jpg'
            thumb_url 'http://example.com/path/to/thumb.png'
            footer 'Slack API'
            footer_icon 'https://platform.slack-edge.com/img/default_application_icon.png'
            ts 123456789
        }

    ])
}
println json.toPrettyString()

您可以在线快速尝试 demo

答案 1 :(得分:0)

此Jenkins文件有效:

#!/bin/groovy

node ('mynode') {
    stage ("Build") {
        def attachments = """[ { \"text\": \"And here’s an attachment!\" } ]"""
        echo (attachments)
        slackSend (channel: "@rolf", color: "danger", message: "Test message", attachments: attachments)
    }
}

这不是:

#!/bin/groovy

node ('mynode') {
    stage ("Build") {
        def attachments = """
[ { \"text\": \"And here’s an attachment!\" } ]"""
        echo (attachments)
        slackSend (channel: "@rolf", color: "danger", message: "Test message", attachments: attachments)
    }
}

所以答案是:确保附件字符串不以换行符开头。

相关问题