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 {<br>
Name string `json:"name"`<br>
Age int `json:"age"`<br>
}
<br>
func main() {<br>
var (<br>
newPer Person<br>
bts []byte<br>
)<br>
per:=Person{<br>
Name: "Lily",<br>
Age: 29,<br>
}<br>
// Serialization<br>
if bts, err = json.Marshal(per); err !=nil{<br>
log.Fatal(err.Error())<br>
return<br>
}
<br>
if err = json.Unmarshal(bts, &newPer); err !=nil{<br>
log.Fatal(err.Error())<br>
return<br>
}<br>
fmt.Println(newPer)<br>
}
func main() {<br>
var (<br>
smap = map[string]int{<br>
"Age": 28,<br>
"Sex": 1,<br>
"Floor": 12,<br>
}<br>
newSmap = make(map[string]int)<br>
bts []byte<br>
)<br>
if bts, err = json.Marshal(smap); err !=nil {<br>
log.Fatal(err.Error())<br>
return<br>
}<br>
if err = json.Umarshal(bts, &newSmap); err !=nil {<br>
log.Fatal(err.Error())<br>
return<br>
}<br>
fmt.Println(newSmap)<br>
}
func main() {<br>
var (<br>
ages []int = []int{23, 20, 28, 25, 30}<br>
bts []byte<br>
err error<br>
newAges = make([]int, len(ages))<br>
)<br>
if bts, err = json.Marshal(ages); err != nil {<br>
fmt.Println(err.Error())<br>
return<br>
}<br>
if err = json.Unmarshal(bts, &newAges); err != nil {<br>
fmt.Println(err.Error())<br>
return<br>
}<br>
fmt.Println("new ages: ", newAges)<br>
return<br>
}
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 {<br>
Name string `json:"name,omitempty"`<br>
Age int `json:"age"`<br>
}<br>
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!