Best way to unmarshal this json?

polaris · · 490 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;ve been learning Go recently, and I&#39;ve primarily been messing around with data from open json feeds to get a feel for things. What&#39;s common practice for dealing with a json blob like this (screenshot as I couldn&#39;t get the json to format here):</p> <p><a href="http://imgur.com/a/lJfR2" rel="nofollow">http://imgur.com/a/lJfR2</a></p> <p>Previously I was experimenting with a separate, unrelated json file where I only wanted a few of the fields, and I created a struct to hold those fields. In this case however, it would be nice to have some kind of data structure to hold this information in. Would a map be best here? If so, how would that work having many &#34;name&#34; keys for various players?</p> <p>So far I&#39;ve tried this (with that blob being a byte array to test on):</p> <pre><code> var m interface{} err := json.Unmarshal(b, &amp;m) if err != nil { fmt.Println(err) } else { f := m.(map[string]interface{}) fmt.Println(f) // to see what the map looks like } </code></pre> <p>However, this doesn&#39;t seem to output sensible intelligible key-val pairings. </p> <p>Since I know the structure of the json, should I make a struct to hold the information? Ideally I could parse things somewhat separately, possibly a struct for individual players, a struct for the total team stats (at the bottom of the json blob above), etc. Would that be a suitable approach? Sorry for the long post, thanks in advance for any pointers.</p> <hr/>**评论:**<br/><br/>SportingSnow21: <pre><p>A quick way to get an idea of the go structure is mholt&#39;s <a href="https://mholt.github.io/json-to-go/" rel="nofollow">json-to-go</a> generator. </p> <p>After a quick look, I think you could build out structs for each stat type, then unmarahal into map[string]&lt;satType struct&gt; with the team/game stats going into structs as well.</p></pre>ConfuciusBateman: <pre><p>Woah... this is amazing. A set of structs like this is exactly what I was hoping to find a way to do. It almost feels like cheating to use this... hahah. Thanks for the tip!</p></pre>xiegeo: <pre><p>instead of </p> <pre><code>var m interface{} json.Unmarshal(b, &amp;m) f := m.(map[string]interface{}) </code></pre> <p>do this <a href="https://play.golang.org/p/CU3vlKvZ8k" rel="nofollow">https://play.golang.org/p/CU3vlKvZ8k</a></p> <pre><code>var m map[string]interface{} json.Unmarshal([]byte(`{&#34;Name&#34;: &#34;Quoll&#34;, &#34;Order&#34;: &#34;Dasyuromorphia&#34;, &#34;id&#34;:1}`), &amp;m) fmt.Printf(&#34;%+v&#34;, m) </code></pre> <p>see also example in <a href="https://golang.org/pkg/encoding/json/#Unmarshal" rel="nofollow">https://golang.org/pkg/encoding/json/#Unmarshal</a> which uses a struct instead of map[string]</p></pre>icholy: <pre><p>Please don&#39;t post pictures of code ...</p></pre>ConfuciusBateman: <pre><p>Sorry about that. I couldn&#39;t get the json chunk to format correctly. I&#39;ll figure it out for next time in case I ever have to post something like that again.</p></pre>joncalhoun: <pre><p>Worst case create a github gist or use pastebin and link to that. <a href="https://gist.github.com/" rel="nofollow">https://gist.github.com/</a></p> <p>At the very least we can copy/paste it then :)</p></pre>ConfuciusBateman: <pre><p>That&#39;s a great idea, I&#39;ll do that if Reddit&#39;s code formatting is being annoying. Thanks!</p></pre>Something6969666: <pre><p>Easiest option is to take your json and use the json to go converter to generate a struct then unmarshal ti the struct.</p> <p>Otherwise create a map[string]interface, unmarshal to that and then type assert shit when you need the values. Do not recommend this as it&#39;s a headache a lot of times </p></pre>ConfuciusBateman: <pre><p>In doing some reading about iterating through structs and stuff, it seems that it can be inefficient. Is it still the most common approach to getting json into a Go program so that you can query it/manipulate it? Ideally I&#39;d want fast lookup time, so if I&#39;m working with a set of nested structs, would this be a problem? </p> <p>I did try the map[string]interface method, and yeah it looked like a mess. The struct way looks super well organized and easy to reason about, but I&#39;m just curious about the efficiency if I&#39;m putting a lot of data into it.</p></pre>Something6969666: <pre><p>Unmarshalling to a struct us probably the most efficient because you&#39;re not having to spend time type asserting.</p> <p>You do actually have a 2b option I forgot to mention...take a look at the &#34;Gabs&#34; package. It works similar to the map string interface but the package gices you the ability to get the values out of your json with the key. The package does the heavy lifting in assertion</p> <p>I&#39;m not sure there&#39;s another way to handle json unmarshalling.</p></pre>

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

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