更新时间:2023-02-25 14:34:36
package main
import (
"bytes"
"encoding/json"
"fmt"
)
type Search struct {
Query string `json:"query"`
}
func main() {
data := &Search{Query: "http://google.com/?q=***&ie=UTF-8"}
responseJSON, _ := JSONMarshal(data, true)
fmt.Println(string(responseJSON))
}
func JSONMarshal(v interface{}, safeEncoding bool) ([]byte, error) {
b, err := json.Marshal(v)
if safeEncoding {
b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1)
b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1)
b = bytes.Replace(b, []byte("\\u0026"), []byte("&"), -1)
}
return b, err
}
结果:
JSONMarshal(data, true)
{"query":"http://google.com/?q=***&ie=UTF-8"}
JSONMarshal(data, false)
{"query":"http://google.com/?q=***\u0026ie=UTF-8"}
: https://github.com/clbanning/mxj/blob/master/ json.go#L20
Playbook: http://play.golang.org/p/c7M32gICl8
Playbook: http://play.golang.org/p/c7M32gICl8