Help: Structure JSON correctly

agolangf · · 909 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey,</p> <p>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 &#39;/meme badLuckBrian, top text, bottom_text&#39;.</p> <p>I&#39;m fine with most of it and have individual bits working but I&#39;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).</p> <p>See below and <a href="http://play.golang.org/p/W9A3LQ0QCf">http://play.golang.org/p/W9A3LQ0QCf</a> to see my issue.</p> <p>Currently returning {&#34;text&#34;:&#34;foo&#34;,&#34;username&#34;:&#34;baz&#34;,&#34;channel&#34;:&#34;bar&#34;,&#34;attachments&#34;:&#34;[{&#39;image_url&#39;: &#39;<a href="http://i.imgflip.com/1bij.jpg&#39;%7D%5D%22">http://i.imgflip.com/1bij.jpg&#39;}]&#34;</a>}</p> <p>Need to return {&#34;text&#34;:&#34;foo&#34;,&#34;username&#34;:&#34;baz&#34;,&#34;channel&#34;:&#34;bar&#34;,&#34;attachments&#34;:[{&#39;image_url&#39;: &#39;<a href="http://i.imgflip.com/1bij.jpg&#39;%7D%5D">http://i.imgflip.com/1bij.jpg&#39;}]</a>}</p> <p>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.</p> <p>Like I said, new to Go and just picking up things I&#39;ve seen in GitHub repos.</p> <p>Advice appreciated. Count me as a noob.</p> <p>Cheers, Adam.</p> <p>___<strong><em>EDIT</em></strong>___</p> <p>Thanks everyone for the help. Went with the approach of treating Attachment as it&#39;s own struct as Slack Attachments can have other attributes than image_url.</p> <p>Cheers! Adam.</p> <hr/>**评论:**<br/><br/>mwholt: <pre><p>&lt; shameless plug for <a href="https://mholt.github.io/json-to-go/">https://mholt.github.io/json-to-go/</a> &gt;</p></pre>drummeradam89: <pre><p>Aha! Shameless plug accepted - this really helps. Cheers!</p></pre>alexwhoizzle: <pre><blockquote> <p><a href="https://mholt.github.io/json-to-go/" rel="nofollow">https://mholt.github.io/json-to-go/</a></p> </blockquote> <p>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. </p> <p>Really liking caddy too! </p></pre>mwholt: <pre><p>Hmmm, I don&#39;t, but that&#39;s a cool idea. I recommend using <a href="http://papaparse.com" rel="nofollow">Papa Parse</a> for parsing the CSV sample. (There I go again - another shameless plug!)</p> <p>(Thanks for your comment, by the way - glad you like it.)</p></pre>uncle_bad_touches: <pre><p>Try this: <a href="http://play.golang.org/p/cTHMaYLyhY" rel="nofollow">http://play.golang.org/p/cTHMaYLyhY</a></p></pre>jayposs: <pre><p>This solution should work as well. </p> <pre><code>type attachment struct { ImageUrl string `json:&#34;image_url&#34;` } type slackMessage struct { Text string `json:&#34;text&#34;` Username string `json:&#34;username&#34;` Channel string `json:&#34;channel&#34;` Attachments []attachment `json:&#34;attachments&#34;` } slackAttachments := make([]attachment, cnt) // cnt = # of attachments // if you have an array (slice) of url&#39;s, use a for loop slackAttachments = append(slackAttachments, attachment{&#34;url1&#34;}) slackAttachments = append(slackAttachments, attachment{&#34;url2&#34;}) set payload slackMessage.Attachments = slackAttachments </code></pre></pre>jasonrichardsmith: <pre><p>Depending on your use case I would make it more expressive. <a href="http://play.golang.org/p/K4BST_Pm2k" rel="nofollow">http://play.golang.org/p/K4BST_Pm2k</a></p></pre>drummeradam89: <pre><p>This works well for this use case as there are other attributes to an attachment that could be used. Cheers!</p></pre>

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

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