Help: Structure JSON correctly

agolangf · 2015-06-17 22:53:19 · 977 次点击    
这是一个分享于 2015-06-17 22:53:19 的资源,其中的信息可能已经有所发展或是发生改变。

Hey,

New to Go, and decided to build a slack bot which interacts with a meme generator site such as imgflip to post a meme with top and bottom text specified such as '/meme badLuckBrian, top text, bottom_text'.

I'm fine with most of it and have individual bits working but I'm struggling with structuring the JSON correctly in my request to the Slack webhook when posting attachments - an array of hashes. So I can get it posting the a message to Slack but not an attachment (in Go that is; I have used the API successfully via Curl and Ruby).

See below and http://play.golang.org/p/W9A3LQ0QCf to see my issue.

Currently returning {"text":"foo","username":"baz","channel":"bar","attachments":"[{'image_url': 'http://i.imgflip.com/1bij.jpg'}]"}

Need to return {"text":"foo","username":"baz","channel":"bar","attachments":[{'image_url': 'http://i.imgflip.com/1bij.jpg'}]}

The obvious issue here is Attachment is a string, but I am unsure of what data type I should be using in the struct, or whether my approach is correct at all.

Like I said, new to Go and just picking up things I've seen in GitHub repos.

Advice appreciated. Count me as a noob.

Cheers, Adam.

EDIT

Thanks everyone for the help. Went with the approach of treating Attachment as it's own struct as Slack Attachments can have other attributes than image_url.

Cheers! Adam.


评论:

mwholt:

< shameless plug for https://mholt.github.io/json-to-go/ >

drummeradam89:

Aha! Shameless plug accepted - this really helps. Cheers!

alexwhoizzle:

https://mholt.github.io/json-to-go/

Do you know of something like this for csv files? If not then I might try to make of port of json-to-go for csv.

Really liking caddy too!

mwholt:

Hmmm, I don't, but that's a cool idea. I recommend using Papa Parse for parsing the CSV sample. (There I go again - another shameless plug!)

(Thanks for your comment, by the way - glad you like it.)

uncle_bad_touches:

Try this: http://play.golang.org/p/cTHMaYLyhY

jayposs:

This solution should work as well.

type attachment struct {
    ImageUrl   string   `json:"image_url"`
}
type slackMessage struct {
    Text            string `json:"text"`
    Username    string `json:"username"`
    Channel       string `json:"channel"`
    Attachments []attachment `json:"attachments"`
}
slackAttachments := make([]attachment, cnt)  // cnt = # of attachments
// if you have an array (slice) of url's, use a for loop
slackAttachments = append(slackAttachments, attachment{"url1"})
slackAttachments = append(slackAttachments, attachment{"url2"})
set payload slackMessage.Attachments = slackAttachments
jasonrichardsmith:

Depending on your use case I would make it more expressive. http://play.golang.org/p/K4BST_Pm2k

drummeradam89:

This works well for this use case as there are other attributes to an attachment that could be used. Cheers!


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

977 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传