<p>So i want to marshal data to JSON. The basic struct looks like this:</p>
<pre><code>type DatabaseObject struct {
Preferences []int `json:"preferences"`
Texts map[string]string `json:"texts"`
Options map[string]string `json:"options"`
Gender string `json:"gender"`
EMail string `json:"email"`
}
</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("%+v", 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's an empty struct, it should look like this</p>
<pre><code>{"preferences":null,"texts":null,"options":null,"gender":"","email":""}
</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('');
</code></pre>
<p>Try changing <code>"%+v"</code> to <code>"%s"</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}
{"preferences":[0,1,2,3],"texts":{},"options":{},"gender":"male","email":"mail@x.com"}
Program exited.
</code></pre>
<p>"</p></pre>demitriousk: <pre><p>json.Marshal returns []byte, and error. you'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'll get the appropriate data. The thing here is that for some intents and purposes []byte is "like" a string, and for others it'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 "part of a character" 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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传