0%

go-json-marshal

golang json.Marshal() html特殊字符会转义 解决方法

go 语言提供了 JSON 的编解码包,JSON 字符串作为参数值传输时发现,json.Marshal 生成 JSON 特殊字符 <、>、&会被转义。

1
2
3
4
5
6
7
8
9
10
11
12
type Test struct {
Content string
}

func main() {
t := new(Test)
t.Content = "http://www.baidu.com?id=123&test=1"
jsonByte, _ := json.Marshal(t)
fmt.Println(string(jsonByte))
}
{"Content":"http://www.baidu.com?id=123\u0026test=1"}
Process finished with exit code 0

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,会转义 <、>、&

1
2
3
4
5
6
7
8
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
2
3
content = strings.Replace(content, "\\u003c", "<", -1)
content = strings.Replace(content, "\\u003e", ">", -1)
content = strings.Replace(content, "\\u0026", "&", -1)

SetEscapeHTML(false)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
type Test struct {
Content string
}

func main() {
t := new(Test)
t.Content = "http://www.baidu.com?id=123&test=1"
bf := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(bf)
jsonEncoder.SetEscapeHTML(false)
jsonEncoder.Encode(t)
fmt.Println(bf.String())
}
{"Content":"http://www.baidu.com?id=123&test=1"}
Process finished with exit code 0

a generic solution for any struct

1
2
3
4
5
6
7
func JSONMarshal(t interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(t)
return buffer.Bytes(), err
}