dqcqcqwq38860 2016-05-31 19:50
浏览 239
已采纳

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!

  • 写回答

1条回答 默认 最新

  • 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))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图