Introduction | The full name of json is Javascript object notation, and the full Chinese name is: js object notation. Among the serialization and deserialization protocols, there are: json, xml, yaml, protocol buffer, etc. Among them, json is the main transmission form of front-end and back-end API contract data. json supports four data types: numeric values, Boolean values, arrays and objects. With these four data types, complex data models can be built. |
In the Go language, we can use the json standard library to implement data serialization and deserialization. This library provides convenient methods to serialize and deserialize maps, structures, arrays, slices, and built-in primitive data types. By using the json standard library we can easily convert the data into json format and parse it back to the original data type when needed. This provides us with great convenience in processing data.
Here, I will give three examples to illustrate the use of json. For beginners, you may often use only one structure object to receive and send data. Let's take a look at specific examples below. 1. Use a structure object to receive data: ``` type Person struct { Name string `json:"name"` Age int `json:"age"` } func main() { data := `{"name": "Alice", "age": 25}` var p Person err := json.Unmarshal([]byte(data), &p) if err != nil { fmt.Println("Failed to parse json:", err) return
Structuretype Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
var (
newPer Person
bts []byte
)
per:=Person{
Name: "Lily",
Age: 29,
}
// Serialization
if bts, err = json.Marshal(per); err !=nil{
log.Fatal(err.Error())
return
}
if err = json.Unmarshal(bts, &newPer); err !=nil{
log.Fatal(err.Error())
return
}
fmt.Println(newPer)
}
func main() {
var (
smap = map[string]int{
"Age": 28,
"Sex": 1,
"Floor": 12,
}
newSmap = make(map[string]int)
bts []byte
)
if bts, err = json.Marshal(smap); err !=nil {
log.Fatal(err.Error())
return
}
if err = json.Umarshal(bts, &newSmap); err !=nil {
log.Fatal(err.Error())
return
}
fmt.Println(newSmap)
}
func main() {
var (
ages []int = []int{23, 20, 28, 25, 30}
bts []byte
err error
newAges = make([]int, len(ages))
)
if bts, err = json.Marshal(ages); err != nil {
fmt.Println(err.Error())
return
}
if err = json.Unmarshal(bts, &newAges); err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("new ages: ", newAges)
return
}
Beginners are relatively clear about json serialization and deserialization of structures, but they may rarely or never use the latter two.
Here we also need to explain the features that I sometimes use when using a structure. The tag value feature omitempty supported by the json of the structure, for example:type Person struct {
Name string `json:"name,omitempty"`
Age int `json:"age"`
}
It means that when the tag value of function omitempty is set, if the data element value of the corresponding structure is zero, it will not be output. That is to say, if an element in the structure data returned by the server is empty and this element is not returned to the front end, you can use the json tag value attribute of omitempty.
Here we provide you with a json parsing library written by Taowen, a senior engineer of Didi Company, which is the fastest in the world. github address: jsoniter.
The above is the detailed content of Serialization and deserialization of json data. For more information, please follow other related articles on the PHP Chinese website!