+ 收藏我们

网站模板

网站模板搜索
404模板 营销型模板 外贸网站模板 单页模板 双语模板 标签大全
电话:18630701785
首页 > 站长学院 > Go语言 json.Marshal 转义html特殊字符(&、)问题及解决 >

Go语言 json.Marshal 转义html特殊字符(&、)问题及解决

时间:2024-04-12 09:17:46

Go 语言解析生成 JSON 字符时,我们可以使用 json.Marshal() 来转换解析,但是如果生成的 JSON 字符串中含有特殊的字符如 <、> 和 & 时候那么他们将会被转义。

问题描述
type Test struct {
    Content     string
}
func main() {
    t := new(Test)
    t.Content = "https://www.baidu.com?q=1&page=1"
    jsonByte, _ := json.Marshal(t)
    fmt.Println(string(jsonByte))
}
输出结果如下,其中 & => \u0026

{"Content":"Https://www.baidu.com?q=1\u0026page=1"}
通过查询文档 GoDoc 说明如下:

String values encode as JSON strings coerced to valid UTF-8,
replacing invalid bytes with the Unicode replacement rune.
The angle brackets “<” and “>” are escaped to “\u003c” and “\u003e”
to keep some browsers from misinterpreting JSON output as html.
Ampersand “&” is also escaped to “\u0026” for the same reason.
This escaping can be disabled using an Encoder that had SetEscapeHTML(false) alled on it.

json.Marshal 默认 escapeHtml 为 true,会转义 <、>、&

func Marshal(v interface{}) ([]byte, error) {
    e := &encodeState{}
    err := e.marshal(v, encOpts{escapeHTML: true})
    if err != nil {
        return nil, err
    }
    return e.Bytes(), nil
}
解决方法
1.字符串替换
c = strings.Replace(c, "\\u003c", "<", -1)
c = strings.Replace(c, "\\u003e", ">", -1)
c = strings.Replace(c, "\\u0026", "&", -1)
这种方式干脆直接,但是批量的字符串替换。还是比较麻烦的。

2. 显示设置 SetEscapeHTML
文档中提到了: This escaping can be disabled using an Encoder that had SetEscapeHTML(false) alled on it.

我们可以通过创建 buffer 存储 json,再创建一个 jsonencoder 通过设置 SetEscapeHTML 编码为 false

type Test struct {
    Content     string
}
func main() {
    t := new(Test)
    t.Content = "https://www.baidu.com?id=1&page=1"
    bf := bytes.NewBuffer([]byte{})
    jsonEncoder := json.NewEncoder(bf)
    jsonEncoder.SetEscapeHTML(false)
    jsonEncoder.Encode(t)
    fmt.Println(bf.String())
}
输出结果:

{"Content":"https://www.baidu.com?id=1&page=1"}

有问题可以加入网站技术QQ群一起交流学习

本站会员学习、解决问题QQ群(691961965)

客服微信号:lpf010888

pbootcms教程

织梦教程

站长学院

SEO

wordpress

竞价教程

信息流

Title