Trouble unmarshaling JSON containing map as a key value

blov · · 575 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I know that in Go you can take a key value pair that are both strings:</p> <pre><code>&#34;foo&#34;: &#34;1&#34; </code></pre> <p>and easily convert the value to say, an int64 during unmarshaling by defining the attribute in your struct like:</p> <pre><code>Foo int64 `json:&#34;foo,string&#34;` </code></pre> <p>What if your value is a map though? I have a map that looks like this in my JSON:</p> <pre><code>&#34;data&#34;: { &#34;foo&#34;: &#34;1&#34;, &#34;bar&#34;: &#34;2&#34;, &#34;baz&#34;: &#34;3&#34;, } </code></pre> <p>The attribute in my struct is defined like this:</p> <pre><code>Data map[string]int64 `json:&#34;data&#34;` </code></pre> <p>That doesn&#39;t work though, because Go sees the values in this map as strings.</p> <p>Is there any way to convert the values to int64s during unmarshaling, or am I going to have to just convert them to int64s when I need to down the road?</p> <hr/>**评论:**<br/><br/>jerf: <pre><p>One option is to define an <a href="https://golang.org/pkg/encoding/json/#Unmarshaler">UnmarshalJSON</a> method on your map type. In that function, you&#39;ll use encoding/json to unmarshal into a temporary <code>map[string]string</code>, then convert everything over. It&#39;s not even necessarily <em>all</em> that wasteful... it ends up overallocating a bit, but it&#39;s what the encoding library would be doing anyways other than that, because it&#39;s all <code>[]byte</code> to start.</p> <p>But I&#39;d also put out there that <code>encoding/json</code> is designed more for ease-of-use and common cases than absolute power. It is not out of the question that you&#39;ll have to go get something from github or something. I don&#39;t think you&#39;re there yet, it&#39;s just something you should have in the back of your mind... it&#39;s not a crime to not fit into encoding/json&#39;s view of the world.</p></pre>ilgooz: <pre><p><a href="http://play.golang.org/p/-gWdAsjSwM">http://play.golang.org/p/-gWdAsjSwM</a> would be similar solution with better performance while decoding big amount of data but requires custom int64 type</p></pre>aminoglycine: <pre><p>Oh, neat. Thanks!</p></pre>jerf: <pre><p>Yes, that&#39;s better.</p></pre>Eggbert345: <pre><p>You can also define one of the attributes as a struct in your definition:</p> <pre><code>type T struct { Data struct { Foo int64 `json:&#34;foo,string&#34;` Bar int64 `json:&#34;bar,string&#34;` Baz int64 `json:&#34;baz,string&#34;` } `json:&#34;data&#34;` } </code></pre></pre>akarl: <pre><p>TIL json tag &#34;string&#34; exists, thank you! If only there were a way to handle an int that might sometimes be encoded as an int and other times as a string for those fun times consuming data marshaled by php...</p></pre>danredux: <pre><p>Had that problem at my old job... Strangely, ffjson handles that exact scenario, and is way way faster.</p></pre>ediskradeht: <pre><p>Looks like your question was answered; however, I want to show anyone who is here a neat site I stumbled across for making a Go struct from copy/pasted json. </p> <p><a href="http://mholt.github.io/json-to-go/" rel="nofollow">http://mholt.github.io/json-to-go/</a></p></pre>

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

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