Search code examples
jsongohtml-escape-charactershtml-escape

Go template HTMLEscape json data and it show """ issue


i try to put json data to web, i use json.Marshal to create json data.

Flowing picture is fmt.Println(string(jsonOut)) result

enter image description here

i use template.HTMLEscape(w, []byte(jsonOut)) to show in web, it will show like following picture.

enter image description here

the " become &#34.

why it will show &#34 and how can i do for show "?


Solution

  • template.HTMLEscape will escape special character.

    use following code can post json data to web

    w.Header().Set("Content-Type", "application/json")
    w.Write(jsonOut)
    

    reference https://www.alexedwards.net/blog/golang-response-snippets#json