go 语言提供了 JSON 的编解码包,JSON 字符串作为参数值传输时发现,json.Marshal 生成 JSON 特殊字符 <、>、&会被转义。
1 2 3 4 5 6 7 8 9 10 11 12
type Test struct { Content string }
funcmain() { 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.