Go json 特殊字符转换(2019.10.24)

Go的 <  >  &等特殊字符转为Json串时会因为为了避免被识别为Html文本(<和>) 会进行特殊转码,所以在生成json串后,可以自行转换

msgBytes = bytes.Replace(msgBytes, []byte("\\u003c"), []byte("<"), -1)
msgBytes = bytes.Replace(msgBytes, []byte("\\u003e"), []byte(">"), -1)
msgBytes = bytes.Replace(msgBytes, []byte("\\u0026"), []byte("&"), -1)



// Replace returns a copy of the slice s with the first n
// non-overlapping instances of old replaced by new.
// If old is empty, it matches at the beginning of the slice
// and after each UTF-8 sequence, yielding up to k+1 replacements
// for a k-rune slice.
// If n < 0, there is no limit on the number of replacements.
func Replace(s, old, new []byte, n int) []byte {

 

 


参考链接:

https://blog.csdn.net/lihao19910921/article/details/81534286

你可能感兴趣的:(GO)