How to stop json.Marshal from escaping < and>?

后端 未结 5 578
甜味超标
甜味超标 2020-12-24 12:48
package main

import \"fmt\"
import \"encoding/json\"

type Track struct {
    XmlRequest string `json:\"xmlRequest\"`
}

func main() {
    message := new(Track)
            


        
5条回答
  •  悲哀的现实
    2020-12-24 13:48

    In Go1.7 the have added a new option to fix this:

    encoding/json: add Encoder.DisableHTMLEscaping This provides a way to disable the escaping of <, >, and & in JSON strings.

    The relevant function is

    func (*Encoder) SetEscapeHTML
    

    That should be applied to a Encoder.

    enc := json.NewEncoder(os.Stdout)
    enc.SetEscapeHTML(false)
    

    Simple example: https://play.golang.org/p/SJM3KLkYW-

提交回复
热议问题