<p>I've been learning Go recently, and I've primarily been messing around with data from open json feeds to get a feel for things. What's common practice for dealing with a json blob like this (screenshot as I couldn'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 "name" keys for various players?</p>
<p>So far I've tried this (with that blob being a byte array to test on):</p>
<pre><code> var m interface{}
err := json.Unmarshal(b, &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'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'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]<satType struct> 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, &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(`{"Name": "Quoll", "Order": "Dasyuromorphia", "id":1}`), &m)
fmt.Printf("%+v", 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't post pictures of code ...</p></pre>ConfuciusBateman: <pre><p>Sorry about that. I couldn't get the json chunk to format correctly. I'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's a great idea, I'll do that if Reddit'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'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'd want fast lookup time, so if I'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'm just curious about the efficiency if I'm putting a lot of data into it.</p></pre>Something6969666: <pre><p>Unmarshalling to a struct us probably the most efficient because you'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 "Gabs" 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'm not sure there's another way to handle json unmarshalling.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传