How can I turn map[string]interface{} into json for output?

blov · · 605 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>When I try json.Marshal it gives me an error saying that it is an unsupported type.<br/> Searched the web and haven&#39;t been able to find an example of this. Some help would be greatly appreciated. Or just point me to an existing example.<br/> Thanks in advance. </p> <p><strong>Edit:</strong> it seems as though the main map isn&#39;t the issue. It is that one of the underlying interfaces is of type map[interface{}]interface{}. I guess I&#39;m going to have to loop over it and rebuild it so that it will work.</p> <hr/>**评论:**<br/><br/>jimpeak: <pre><p><a href="http://play.golang.org/p/wRFOXy6zzd">http://play.golang.org/p/wRFOXy6zzd</a></p></pre>wirther: <pre><p>Expanding on this, here are examples where unsupported types fail: <a href="http://play.golang.org/p/7k9vDTcLgC" rel="nofollow">http://play.golang.org/p/7k9vDTcLgC</a></p></pre>Darfk: <pre><p>You need to take a look at the <a href="http://blog.golang.org/json-and-go" rel="nofollow">json and go</a> blog post. If this cannot help you then you ought to post your code and compiler/program output so we can help debug. </p></pre>alexisnotonfire: <pre><p>The thing no one is mentioning is the fact that a map[string]interface{} is almost never what you want to use for any real code, use json tagged structs to represent your data.</p></pre>bigfoot13442: <pre><p>Unfortunately the struct is defined in another package.</p></pre>alexisnotonfire: <pre><p>that&#39;s unfortunate, i remember seeing someone working with a rethinkdb lib the other day where every single return type was interface{}, poor fucker.</p></pre>dashausSP: <pre><p>LOL</p></pre>drvd: <pre><p>That depends. A nil map[string]interface{} encodes without problem to JSON :-)</p></pre>

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

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