Skip to content

JSON Marshal 转义问题

Updated: at 07:22 AMSuggest Changes

转义

func main() {
	testMap := map[string]string{
		"demo": `https://xxx.xxx.com?a=1&b=2`,
	}
	bytes, err := json.Marshal(testMap)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(bytes))
}

// 输出:{"demo":"https://xxx.xxx.com?a=1\u0026b=2"}

不转义

func main() {
	testMap := map[string]string{
		"demo": `https://xxx.xxx.com?a=1&b=2`,
	}
	byteBuf := bytes.NewBuffer([]byte{})
	encoder := json.NewEncoder(byteBuf)
	encoder.SetEscapeHTML(false)
	err := encoder.Encode(testMap)
	if err != nil {
		panic(err)
	}
	fmt.Println(byteBuf.String())
}

// 输出:{"demo":"https://xxx.xxx.com?a=1&b=2"}

Previous Post
Antd 混用 bulma 的坑
Next Post
Python setup 常用命令