Golang is a very popular programming language and is widely used in fields such as network servers, Internet big data processing, and algorithm development. Among them, Golang provides a complete mechanism for data serialization and deserialization, the most important of which is support for JSON format data. This article will explore how Golang implements serialization and deserialization of JSON data.
1. Introduction to JSON
JSON stands for JavaScript Object Notation, a lightweight data exchange format that is easy to read, write and parse. It was first proposed by Douglas Crockford in 2001 and formally defined in RFC 7159. Data in JSON format is composed of key-value pairs. The key name is a string, and the value can be a string, number, Boolean value, array, object, and null.
The basic format of JSON data is as follows:
{ "name": "Jack", "age": 30, "address": { "city": "New York", "zip": 10001 }, "hobbies": ["Swimming", "Reading"] }
2. Golang’s JSON processing
Golang provides processing of JSON data through the “encoding/json” package in the standard library processing support. This package contains functions for encoding and decoding JSON, which allow us to convert struct (structure type) in Go language to JSON byte array or vice versa.
Before using this package, you need to understand some basic concepts.
In Golang, the process of converting Go language data types to JSON format data is called Marshal, and JSON data is converted to Go language The process of data type is called Unmarshal.
In Golang, fields in structure types can be annotated through structure tags (Struct Tag). This tag can customize the name, type, and size of the field when encoding and decoding JSON.
An example is as follows:
type Person struct { Name string `json:"name"` Age int `json:"age"` Address string `json:"address"` }
In this example, the Person structure contains three fields: Name, Age and Address. Since the standard naming method of JSON does not match the lowercase first letter commonly used in the Go language, we need to use the json
tag to tell the JSON encoding and decoding function which field name to use when encoding data.
3. Encoding of JSON data
In Golang, the process of converting data types in the Go language into JSON format data is called Marshal. This process utilizes the function "json.Marshal()" in the "encoding/json" package.
The following is an example of using the json.Marshal() function to encode data types in Golang into JSON formatted data:
package main import ( "encoding/json" "fmt" ) type Person struct { Name string `json:"name"` Age int `json:"age"` Address string `json:"address"` } func main() { p := Person{ Name: "Tom", Age: 20, Address: "Shanghai", } data, err := json.Marshal(p) if err != nil { fmt.Println("Json marshal error:", err) return } fmt.Println(string(data)) }
The output results are as follows:
{"name":"Tom","age":20,"address":"Shanghai"}
The above In the code, we define a structure type Person and give each field of this structure type a json tag. In the main function, we create an instance p of type Person and use the json.Marshal() function to encode it into JSON format data. The final result is a byte array, which needs to be converted to a string using the string() function.
4. Decoding of JSON data
In Golang, the process of converting JSON format data into Go language data types is called Unmarshal. This process utilizes the function "json.Unmarshal()" in the "encoding/json" package.
The following is an example of using the json.Unmarshal() function to decode JSON formatted data into data types in Golang:
package main import ( "encoding/json" "fmt" ) type Person struct { Name string `json:"name"` Age int `json:"age"` Address string `json:"address"` } func main() { jsonString := `{"name":"Tom","age":20,"address":"Shanghai"}` var p Person err := json.Unmarshal([]byte(jsonString), &p) if err != nil { fmt.Println("Json Unmarshal error:", err) return } fmt.Println(p.Name, p.Age, p.Address) }
The output results are as follows:
Tom 20 Shanghai
The above In the code, we first define a string in JSON format, representing an instance of the Person type. We then use the json.Unmarshal() function to decode it into a data type in Golang. In this process, we must provide an uninitialized variable as the target variable to save the decoding results. In this example, we use a variable of type Person structure named p. This function also accepts a pointer to a byte array as the second parameter to hold the decoded data.
5. Conclusion
This article introduces the method of encoding and decoding JSON data through Golang. This process is very practical when we are dealing with tasks that require interaction with various Internet APIs. In addition, in the process of processing data transmission, it is still very important to master JSON. If you want to learn more about how to process JSON data in Golang, please check the official documentation for the description of the "encoding/json" package.
The above is the detailed content of golang implements json. For more information, please follow other related articles on the PHP Chinese website!