dqcqcqwq38860
dqcqcqwq38860
2016-05-31 19:50
浏览 107

Golang JSON封送将表情符号转换为问号

I have a golang web server which communicates with a MySQL database. Users can post comments, and these comments can contain emoji, among other utf8mb4 characters. My database is capable of storing and retrieving these emoji characters, however, when JSON marshaling / unmarshmaling, emoji are mangled. Is is possible to use Go's native encoding/json package to decode / encode utf8mb4 characters? If so, how would I go about doing so. Thanks in advance!

图片转代码服务由CSDN问答提供 功能建议

我有一个与MySQL数据库通信的golang网络服务器。 用户可以发表评论,这些评论可以包含表情符号以及其他utf8mb4字符。 我的数据库能够存储和检索这些表情符号字符,但是,当JSON封送/取消封送处理时,表情符号将被破坏。 是否可以使用Go的本地编码/ json包对utf8mb4字符进行解码/编码? 如果是这样,我将如何去做。 预先感谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doushaiyu5065
    doushaiyu5065 2016-06-01 08:10
    已采纳

    I was eventually able to find a workaround. Previously I was marshaling the JSON into a []byte and then calling w.Write(jsonBytes) on my http.ResponseWriter. By casting the bytes to a string, writing the appropriate content type header and using io.WriteString instead I was able to prevent the mangling. Here is my code:

    returnJSON, error := json.Marshal(value)
    if error != nil { Error(w, error); return }
    w.Header().Set("Content-Type", "application/json; charset=utf-8")
    io.WriteString(w, string(returnJSON))
    
    点赞 评论

相关推荐