golang-json-

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang struct to json HTML without escape

Default state if struct value is HTML code output to JSON would covert to Unicode

Package Mainimport ("Encoding/json"    "FMT"    "OS") Func main () {type Colorgroupstruct{IDintNamestringColors []string} Group:=colorgroup{ID:1, Name:"Reds", Colors: []string{"","Red","Ruby","Maroon"},} B, err:=JSON. Marshal (group)ifErr! =Nil {fmt. Println ("Error:", Err)} Os. Stdout.write (b)}

Result would 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 Colorgroupstruct{IDintNamestringColors []string} Group:=colorgroup{ID:1, Name:"Reds", Colors: []string{"","Red","Ruby","Maroon"},} B, err:=JSON. Marshal (group)ifErr! =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":["","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 Trackstruct{xmlrequeststring' JSON:"XMLRequest"'}func (t*track) JSON () ([]byte, error) {buffer:= &bytes. buffer{} encoder:=JSON. Newencoder (buffer) encoder. Setescapehtml (false) Err:=Encoder. Encode (t)returnbuffer. 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"    ifERR: = Enc. Encode (&AMP;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 have escaped and recover to HTML code

Package Mainimport (    "fmt"    "html") Func Main () {    const s = ' &quot; Fran &amp; freddie&#; 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.