再过5分钟你就能了解Go语言 json.Marshal 如何处理html特殊字符(&、<、>)啦

744 阅读1分钟

一、问题

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

package main

import (
    "encoding/json"
    "fmt"
)

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

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
)  

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"}