JSON processing in the Golang

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

JSON(Javascript Object Notation) has become a very popular format for data interchange, Golang naturally does not ignore the JSON support, Golang standard library can be easily processed JSON. In addition, we recommend one of the fastest JSON parsers in the world jsoniter .

Brief introduction

The standard package for working with JSON, provided in JSON encoding/json , is mainly used by the following two methods:

// 序列化func Marshal(v interface{}) ([]byte, error)// 反序列化func Unmarshal(data []byte, v interface{}) error

The data structure before and after serialization has the following correspondence:

bool, for JSON booleansfloat64, for JSON numbersstring, for JSON strings[]interface{}, for JSON arraysmap[string]interface{}, for JSON objectsnil for JSON null

Unmarshal

This is a deserialization process that assembles the JSON string into a struct.

Known resolution type

The sample code is as follows:

package mainimport (    "encoding/json"    "fmt")type Animal struct {    Name  string    Order string}func main() {    var jsonBlob = []byte(`[        {"Name": "Platypus", "Order": "Monotremata"},        {"Name": "Quoll",    "Order": "Dasyuromorphia"}    ]`)    var animals []Animal    err := json.Unmarshal(jsonBlob, &animals)    if err != nil {        fmt.Println("error:", err)    }    fmt.Printf("%+v", animals)}

After running, the output results:[{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
As you can see, the struct field name corresponds to KEY one by one in the JSON.
For example, the KEY in JSON is Name, so how to find the corresponding field?

    • First, look for the exportable struct field with the Name in the tag (first capitalization)

    • Next find the field name is the export field of name

    • Finally, look for an export field such as name or name that is not sensitive to case except the first letter.

Note: The field that can be assigned must be an exportable field!!

At the same time JSON parsing will only parse the fields that can be found, the missing fields will be ignored, one of the benefits is: When you receive a very large JSON data structure and you just want to get some of the data, you only need to have the data you want to the field name in uppercase, can easily solve this problem.

Unknown parse type

What I said earlier is that the type that is known to be parsed, for example, when you see the JSON arrays define a Golang array to receive data, and when you see the JSON objects define a map to receive the data, what happens? The answer is to receive using interface{} and then parse with type assert , for example:

var f interface{}b := []byte(`{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}`)json.Unmarshal(b, &f)for k, v := range f.(map[string]interface{}) {    switch vv := v.(type) {    case string:        fmt.Println(k, "is string", vv)    case int:        fmt.Println(k, "is int ", vv)    case float64:        fmt.Println(k, "is float64 ", vv)    case []interface{}:        fmt.Println(k, "is array:")        for i, j := range vv {            fmt.Println(i, j)        }    }}

Marshal

This is the process of serialization, which serializes a struct into a JSON string.
The sample code is as follows:

package mainimport (    "encoding/json"    "fmt")type Animal struct {    Name  string `json:"name"`    Order string `json:"order"`}func main() {    var animals []Animal    animals = append(animals, Animal{Name: "Platypus", Order: "Monotremata"})    animals = append(animals, Animal{Name: "Quoll", Order: "Dasyuromorphia"})    jsonStr, err := json.Marshal(animals)    if err != nil {        fmt.Println("error:", err)    }    fmt.Println(string(jsonStr))}

After running, the output results:

[{"name":"Platypus","order":"Monotremata"},{"name":"Quoll","order":"Dasyuromorphia"}]

It can be found that the serialized JSON string's key name is the same as the name specified after the struct JSON tag.
When there is no JSON tag behind the struct field, the resulting JSON string has the same key name as the field name.
JSON tag has a lot of values to take, and it has different meanings, such as:

    • The tag is "-", indicating that the field is not output to JSON.

    • With a custom name in the tag, the custom name appears in the field name of the JSON, such as the lowercase letter above name .

    • With the "omitempty" option in the tag, if the field value is empty, it is not exported to the JSON string.

    • If the field type is bool, string, int, int64, and so on, and the tag has a "string" option, the field will convert the value of that field to a JSON string when it is output to JSON.

Recommended JSON parsing Library

Jsoniter (Json-iterator) is a fast and flexible JSON parser that offers both Java and Go two versions. Borrowed a lot of code from Dsljson and Jsonparser.

The Golang version of Jsoniter can be as much as 6 times times faster than the standard library (Encoding/json), and this performance is obtained without the use of code generation.

Can be used go get github.com/json-iterator/go to obtain, fully compatible with the standard library Marshal and Unmarshal methods.
When using import github.com/json-iterator/go instead of the standard library, the basic usage is as follows:

jsoniter.Marshal(&data)jsoniter.Unmarshal(input, &data)

Reference

    1. JSON processing

    2. JSON iterator

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.