json.Marshal with strange behaviour

blov · · 402 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>So i want to marshal data to JSON. The basic struct looks like this:</p> <pre><code>type DatabaseObject struct { Preferences []int `json:&#34;preferences&#34;` Texts map[string]string `json:&#34;texts&#34;` Options map[string]string `json:&#34;options&#34;` Gender string `json:&#34;gender&#34;` EMail string `json:&#34;email&#34;` } </code></pre> <p>Here is the (working) Playground version: <a href="https://play.golang.org/p/GI3nAo7L4a" rel="nofollow">https://play.golang.org/p/GI3nAo7L4a</a></p> <p>When i use this code in my program however, the result is very different. Here is my code:</p> <pre><code>jsonObject, err := json.Marshal(DatabaseObject{}) if err != nil { log.Fatal(err) } fmt.Printf(&#34;%+v&#34;, jsonObject) </code></pre> <p>It prints:</p> <pre><code>[123 34 112 114 101 102 101 114 101 110 99 101 115 34 58 110 117 108 108 44 34 116 101 120 116 115 34 58 110 117 108 108 44 34 111 112 116 105 111 110 115 34 58 110 117 108 108 44 34 103 101 110 100 101 114 34 58 34 34 44 34 101 109 97 105 108 34 58 34 34 125] </code></pre> <p>Does anyone know why json.Marshal does not work here? It&#39;s an empty struct, it should look like this</p> <pre><code>{&#34;preferences&#34;:null,&#34;texts&#34;:null,&#34;options&#34;:null,&#34;gender&#34;:&#34;&#34;,&#34;email&#34;:&#34;&#34;} </code></pre> <hr/>**评论:**<br/><br/>letsencrypt: <pre><p>That is exactly what you are getting:</p> <pre><code>[ 123,34,112,114,101,102,101,114,101,110,99,101,115,34,58,110,117,108,108,44,34, 116,101,120,116,115,34,58,110,117,108,108,44,34,111,112,116,105,111,110,115,34, 58,110,117,108,108,44,34,103,101,110,100,101,114,34,58,34,34,44,34,101,109,97, 105,108,34,58,34,34,125 ].map(function (key) { return String.fromCharCode(key); }).join(&#39;&#39;); </code></pre> <p>Try changing <code>&#34;%+v&#34;</code> to <code>&#34;%s&#34;</code> and see if that is what you are expecting.</p> <p><strong>EDIT:</strong> You can also replace <code>fmt.Printf()</code> with <code>os.Stdout.Write()</code>.</p></pre>anxiousalpaca: <pre><p>thank you, that was it!</p></pre>delphi329: <pre><p>works for my on the playground. It prints:</p> <pre><code>{Preferences:[0 1 2 3] Texts:map[] Options:map[] Gender:male EMail:mail@x.com} {&#34;preferences&#34;:[0,1,2,3],&#34;texts&#34;:{},&#34;options&#34;:{},&#34;gender&#34;:&#34;male&#34;,&#34;email&#34;:&#34;mail@x.com&#34;} Program exited. </code></pre> <p>&#34;</p></pre>demitriousk: <pre><p>json.Marshal returns []byte, and error. you&#39;re printing an array of bytes. If you want to see that as a string you could do string(jsonObject) to cast it as such. Alternatively you can write it to any io.Writer (like os.Stdout) since that takes []byte and you&#39;ll get the appropriate data. The thing here is that for some intents and purposes []byte is &#34;like&#34; a string, and for others it&#39;s not. </p> <p>A reason for this is that Go supports utf8... So if you make a string consisting of multi-byte characters and cast it to []byte you can easily get &#34;part of a character&#34; by slicing out the wrong range of bytes.. Which would be invalid utf8 when cast back into a string.</p> <p>My explanation is probably only 1/3 of the story and maybe built upon a foundation of half lies. But it should be enough to get you going without digging into anything super technical.</p></pre>

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

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