dongyuanliao6204 2014-12-15 13:31
浏览 116
已采纳

golang json marshal将标签转换为utf符号

How to prevent converting <p> to \u003cp\u003e in json.Marshal ?

package main

import (
    "encoding/json"
    "fmt"
)

type User struct {
    Name string
}

func main() {
    user := User{Name: "<p>Frank</p>"}
    b, err := json.Marshal(&user)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(user)
    fmt.Println(string(b))
}

It outputs:

{<p>Frank</p>}
{"Name":"\u003cp\u003eFrank\u003c/p\u003e"}

playground

  • 写回答

1条回答 默认 最新

  • dongliping003116 2014-12-15 14:00
    关注

    This is already explained in this question. In short - you can create your own Marshaller implementation if you need to preserve these characters, but even encoded the json output will be a perfectly valid one and should not break anything.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制