<p>I know that in Go you can take a key value pair that are both strings:</p>
<pre><code>"foo": "1"
</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:"foo,string"`
</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>"data": {
"foo": "1",
"bar": "2",
"baz": "3",
}
</code></pre>
<p>The attribute in my struct is defined like this:</p>
<pre><code>Data map[string]int64 `json:"data"`
</code></pre>
<p>That doesn'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'll use encoding/json to unmarshal into a temporary <code>map[string]string</code>, then convert everything over. It's not even necessarily <em>all</em> that wasteful... it ends up overallocating a bit, but it's what the encoding library would be doing anyways other than that, because it's all <code>[]byte</code> to start.</p>
<p>But I'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'll have to go get something from github or something. I don't think you're there yet, it's just something you should have in the back of your mind... it's not a crime to not fit into encoding/json'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'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:"foo,string"`
Bar int64 `json:"bar,string"`
Baz int64 `json:"baz,string"`
} `json:"data"`
}
</code></pre></pre>akarl: <pre><p>TIL json tag "string" 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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传