JSON

Whats the best way to represent float64 as non

字号+ 作者:H5之家 来源:H5之家 2016-01-17 16:20 我要评论( )

pI have a JSON that I don#39;t necessarily know the structure, when unmarshaling it to codemap[string]interface{}/code, all numbers have codefloat64/c

<p>I have a JSON that I don&#39;t necessarily know the structure, when unmarshaling it to <code>map[string]interface{}</code>, all numbers have <code>float64</code> type.</p> <p>Creating the perfect struct to map the values is difficult since I don&#39;t have control over the JSON and this API may change without notice.</p> <p>Many of my values are actually <code>int64</code>, but since they became <code>float64</code> when I print on <code>html/template</code>, they look like this: 3.486912512e+09</p> <p>What&#39;s the best way to represent that number as 3486912512?</p> <hr/>**评论:**<br/><br/>GopherFromHell: <pre><p>you can use a json.Decoder and call the function Decoder.UseNumber() that will unmarshal to a json.Number instead of float64</p> <p><a href="https://golang.org/pkg/encoding/json/#NewDecoder" rel="nofollow">https://golang.org/pkg/encoding/json/#NewDecoder</a> <a href="https://golang.org/pkg/encoding/json/#Decoder.UseNumber" rel="nofollow">https://golang.org/pkg/encoding/json/#Decoder.UseNumber</a> <a href="https://golang.org/pkg/encoding/json/#Number" rel="nofollow">https://golang.org/pkg/encoding/json/#Number</a></p></pre>pinpinbo: <pre><p>Ah, that&#39;s it. Thanks!</p></pre>

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评