Question about json.Unmarshal into struct, only interested in parts of JSON

xuanbao · · 408 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Edit: Solved. I&#39;m an idiot and didn&#39;t know I needed quotes.</p> <p>So I&#39;m having trouble getting this to work properly, hoping Reddit could help. I&#39;m getting some JSON containing Weather info via the Wunderground API. It returns a whole lot of stuff but I&#39;m only interested in various parts of it. In my testing I&#39;ve had mixed results with getting things to unmarshal properly. <a href="https://play.golang.org/p/TwITkH8vAO" rel="nofollow">Playground Link</a> with an example of what&#39;s going on. In the first attempt, nothing is being unmarshalled, and in the second, only 1 value is unmarshalled. I&#39;d like all the values in the struct to be populated, but the JSON contains much more information than what&#39;s in the struct</p> <p>If anyone could give advice on what I&#39;m doing wrong, I&#39;d really appreciate it.</p> <hr/>**评论:**<br/><br/>niosop: <pre><p>If you don&#39;t know about it, <a href="https://mholt.github.io/json-to-go/" rel="nofollow">https://mholt.github.io/json-to-go/</a> is handy as hell. Just paste in the whole JSON and it&#39;ll generate the go structs for you. Then you can delete the parts you don&#39;t care about.</p></pre>Guitarbum722: <pre><p>^ literally the best.</p></pre>popillol: <pre><p>Wow how did I not know about this, this is amazing. Thanks!</p></pre>JHunz: <pre><p>You need quotes around your field names inside the json tag, e.g. `json:&#34;current_observation&#34;`</p></pre>popillol: <pre><p>Oh man for some reason I didn&#39;t think those were necessary. Thanks for the help! I&#39;m ashamed to say I had tried to figure out what was wrong for over an hour or two before asking Reddit</p></pre>TAAnderson: <pre><p>Maybe have a look at github.com/buger/jsonparser if you are only interested in some parts of the json.</p></pre>

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

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