Golang--json Data Processing

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

Today the official documents have abused several streets.

You need to be able to encode the JSON data and convert the internal Chinese string into Unicode encoding. Coding this kind of thing contact also a lot of, casually search can solve. Determined to search for a bit. All codes in this article

rs := []rune("golang中文unicode编码")j := ""html := ""for _, r := range rs {rint := int(r)if rint < 128 {j += string(r)html += string(r)} else {j += "\\u" + strconv.FormatInt(int64(rint), 16) // jsonhtml += "&#" + strconv.Itoa(int(r)) + ";"       // 网页}}fmt.Printf("JSON: %s\n", j)fmt.Printf("HTML: %s\n", html)

Inside the Golang, a single character in all UTF8 strings can be byte represented by a type, and []byte(str) after being converted to an array, it can be traversed. For Unicode,golang, a different data type is provided rune , and []rune(str) after conversion, a single Unicode character can be obtained. For English characters as well as English punctuation, Unicode encoding is unchanged, and the Chinese code is converted into 16 binary.

We generally believe thatUTF-16 is Unicode, in 16-bit two-byte units, with a minimum of two bytes and a maximum of 4 bytes.

So, in general, the transferred Unicode is made up of 4 16 binary digits (2 bytes). Finally, add a header for it to \u complete.

When you're done, you're ready to turn around. When the structure data is stitched up, the items with Chinese are coded separately. Now think about it, hard coding is too slag.

Look at the official documentation:

Marshal traverses the value v recursively. If an encountered value implements the Marshaler interface and are not a nil pointer, Marshal calls its Marshaljson method to produce JSON. The nil pointer exception isn't strictly necessary but mimics a similar, necessary exception in the behavior of Unmarshal Json.

After the method is called json.Marshal , recursively accesses each item of the incoming struct, and if the traversed item implements Marshaler the method, and the item is not a null pointer, the implemented method is automatically called MarshalJSON .

type QuoteString struct {QString string}func (q QuoteString) MarshalJSON() ([]byte, error) {return []byte(strconv.QuoteToASCII(q.QString)), nil}type ColorGroup struct {ID     int         `json:"id,string"`Name   QuoteString `json:"name"`Colors []string}

The above defines a struct QuoteString , which is implemented Marshaler , so it is automatically Unicode encoded. transcoding, using the strconv function in the bag QuoteToASCII . This method should be the upgrade version I provided above.

Source code is very important.

###### References + "1" Package json-the Go Programming language+ "2" Golang Chinese Unicode code-cardamom + "3" package strconv-the Go Programming Language

Original link: Golang--json data processing, reproduced please specify the source!

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.