本文主要是介绍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 {
参考链接:
golang json.Marshal 特殊html字符被转义解决方案_lihao19910921的专栏-CSDN博客_golang 去除转义
这篇关于Go json 特殊字符转换(2019.10.24)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!