1

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 "?

dlsniper
  • 7,188
  • 1
  • 35
  • 44
Peggy
  • 372
  • 1
  • 6
  • 27

2 Answers2

2

If you just want show json in the http response

w.Write(jsonOut)

If you want to show json in html

t, _ := template.New("foo").Parse(`<head></head><body>{{$.data}}</body>`)   
_ = t.Execute(w, map[string]string{
    "data": string(jsonOut),
})
Bryce
  • 3,046
  • 2
  • 19
  • 25
1

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

Peggy
  • 372
  • 1
  • 6
  • 27