golang -json-

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

golang  struct to json  html without  escape 

default state when  struct value is html  code output to json will covert to unicode 

package mainimport (    "encoding/json"    "fmt"    "os")func main() {    type ColorGroup struct {        ID     int        Name   string        Colors []string    }    group := ColorGroup{        ID:     1,        Name:   "Reds",        Colors: []string{"<html> </html>", "Red", "Ruby", "Maroon"},    }    b, err := json.Marshal(group)    if err != nil {        fmt.Println("error:", err)    }    os.Stdout.Write(b)}

 

result will be :

{"ID":1,"Name":"Reds","Colors":["\u003chtml\u003e \u003c/html\u003e","Red","Ruby","Maroon"]}

after  change:

package mainimport (    "encoding/json"    "fmt"    "os"    "bytes")func main() {    type ColorGroup struct {        ID     int        Name   string        Colors []string    }    group := ColorGroup{        ID:     1,        Name:   "Reds",        Colors: []string{"<html> </html>", "Red", "Ruby", "Maroon"},    }    b, err := json.Marshal(group)    if err != nil {        fmt.Println("error:", err)    }            buffer := &bytes.Buffer{}        encoder := json.NewEncoder(buffer)        encoder.SetEscapeHTML(false)        err2 := encoder.Encode(group)        bytes:= buffer.Bytes()         fmt.Println(err2)                os.Stdout.Write(bytes  )        os.Stdout.Write(b)}

https://play.golang.org/p/nWxBIc1Ig0z

result :

<nil>{"ID":1,"Name":"Reds","Colors":["<html> </html>","Red","Ruby","Maroon"]}{"ID":1,"Name":"Reds","Colors":["\u003chtml\u003e \u003c/html\u003e","Red","Ruby","Maroon"]}

 

example2:

package mainimport "fmt"import "encoding/json"import "bytes"type Track struct {    XmlRequest string `json:"xmlRequest"`}func (t *Track) JSON() ([]byte, error) {    buffer := &bytes.Buffer{}    encoder := json.NewEncoder(buffer)    encoder.SetEscapeHTML(false)    err := encoder.Encode(t)    return buffer.Bytes(), err}func main() {    message := Track{}    message.XmlRequest = "<car><mirror>XML</mirror></car>"    fmt.Println("Before Marshal", message)    messageJSON, _ := message.JSON()    fmt.Println("After marshal", string(messageJSON))}

result:

Before Marshal {<car><mirror>XML</mirror></car>}After marshal {"xmlRequest":"<car><mirror>XML</mirror></car>"}

refer: https://play.golang.org/p/FAH-XS-QMC

 

or 

package mainimport (    "encoding/json"    "log"    "bytes"    "fmt")func main() {    buf := new(bytes.Buffer)    enc := json.NewEncoder(buf)    enc.SetEscapeHTML(false)    v := make(map[string]string)    v["key"] = "value with <> symbols"    if err := enc.Encode(&v); err != nil {        log.Println(err)    }    fmt.Printf("json codec: %v", buf.String())}

and result:

json codec: {"key":"value with <> symbols"}

refer :https://play.golang.org/p/SJM3KLkYW- 

 

html code store in mongo has escaped and recover to html code 

package mainimport (    "fmt"    "html")func main() {    const s = `&quot;Fran &amp; Freddie&#39;s Diner&quot; &lt;tasty@example.com&gt;`    fmt.Println(html.UnescapeString(s))}

result:

"Fran & Freddie's Diner" <tasty@example.com>

refer : https://golang.org/pkg/html/#example_UnescapeString

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.