当前位置:网站首页>How to use serialization and deserialization of JSON in golang
How to use serialization and deserialization of JSON in golang
2022-04-21 18:14:00 【Billion speed cloud】
Golang in Json How to use serialization and deserialization of
This article mainly introduces “Golang in Json How to use serialization and deserialization of ”, In daily operation , I believe a lot of people are Golang in Json There are doubts about how to use serialization and deserialization of , Xiao Bian consulted all kinds of materials , Sort out simple and easy-to-use operation methods , I hope to answer ”Golang in Json How to use serialization and deserialization of ” Your doubts help ! Next , Please follow Xiaobian to learn !
JSON:
JSON(JavaScript Object Notation): Is a lightweight data exchange format . It is based on ECMAScript A subset of the specification , Use text format completely independent of programming language to store and represent data .
A simple and clear hierarchy makes JSON Become the ideal data exchange language . Easy to read and write , At the same time, it is also easy to analyze and generate by computer , And effective Improve the efficiency of network transmission .
Json Easy to machine parse and generate , And effectively improve the network transmission efficiency , Usually, when the program transmits, it will first serialize the data into json character string , The receiver is then deserialized into the original data type
All data types can be through Json Express
Json.cn This website can verify Json Format
Serialize with json.Marshal()
Deserialization uses json.Unmarshal(), During deserialization, the data type should be consistent with that before serialization
Create format :

Basic data type serialization :
func testBasic() { num := 1.111 marshal, err := json.Marshal(num) if err != nil { fmt.Println("json.Marshal err:", err) } fmt.Println(" After serialization :", string(marshal)) // After serialization : 1.111}
Structure serialization :
func main() { testStudent()}/**type Student struct { Name string Age int Birthday string Address string}*/// If you add `json:"student_name"`, After serialization, the data field returns the specified format , Can be lowercase ,json Fix , Casual in the back type Student struct { // Variables can only be resolved if they are capitalized Name string `json:"student_name"` Age int `json:"student_age"` Birthday string `json:"student_birthday"` Address string `json:"student_address"`}func testStudent() { student := Student{ Name: "itzhuzhu", Age: 24, Birthday: "1998-01-01", Address: " Tianhe District, Guangzhou ", } marshal, err := json.Marshal(&student) if err != nil { fmt.Println("json.Marshal err:", err) } fmt.Println(" After serialization :", string(marshal)) // After serialization :{"Name":"itzhuzhu","Age":24,"Birthday":"1998-01-01","Address":" Tianhe District, Guangzhou "}}
map serialize :
func testMap() { var m map[string]interface{} m = make(map[string]interface{}) m["name"] = " Han xin " m["age"] = 23 m["address"] = " Guangzhou " marshal, err := json.Marshal(m) if err != nil { fmt.Println("json.Marshal err:", err) } fmt.Println(" After serialization :", string(marshal)) // After serialization : {"address":" Guangzhou ","age":23,"name":" Han xin "}}
Slice serialization :
func testSlice() { var slice []map[string]interface{} var m map[string]interface{} m = make(map[string]interface{}) m["name"] = " Han xin " m["age"] = 23 m["address"] = " Guangzhou " slice = append(slice, m) marshal, err := json.Marshal(m) if err != nil { fmt.Println("json.Marshal err:", err) } fmt.Println(" After serialization :", string(marshal)) // After serialization : {"address":" Guangzhou ","age":23,"name":" Han xin "}}
Deserialize to struct :
func deserialize() { str := "{\"Name\":\"itzhuzhu\",\"Age\":24,\"Birthday\":\"1998-01-01\",\"Address\":\" Tianhe District, Guangzhou \"}" // Use Unmarshal Deserialization var student Student err := json.Unmarshal([]byte(str), &student) if err != nil { fmt.Println("json.Unmarshal err:", err) } fmt.Println(" After deserialization :", student) // After deserialization : {itzhuzhu 24 1998-01-01 Tianhe District, Guangzhou }}
Deserialize to map:
func deserializeMap() { str := " {\"address\":\" Guangzhou \",\"age\":23,\"name\":\" Han xin \"}" // Deserialization does not require make, Encapsulated in Unmarshal It's in var m map[string]interface{} err := json.Unmarshal([]byte(str), &m) if err != nil { fmt.Println("json.Unmarshal err:", err) } fmt.Println(" After deserialization :", m) // map[address: Guangzhou age:23 name: Han xin ]}
Deserialize to slice :
func deserializeSlice() { str := " [{\"address\":\" Guangzhou \",\"age\":23,\"name\":\" Han xin \"}]" var slice []map[string]interface{} err := json.Unmarshal([]byte(str), &slice) if err != nil { fmt.Println("json.Unmarshal err:", err) } fmt.Println(" After deserialization :", slice) // After deserialization : [map[address: Guangzhou age:23 name: Han xin ]]}
Here we are , About “Golang in Json How to use serialization and deserialization of ” That's the end of my study , I hope we can solve your doubts . The combination of theory and practice can better help you learn , Let's try ! If you want to continue to learn more related knowledge , Please continue to pay attention to Yisu cloud website , Xiaobian will continue to strive to bring you more practical articles !
版权声明
本文为[Billion speed cloud]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211811443664.html
边栏推荐
猜你喜欢

MySQL - remote connection to non local MySQL database server, Error 1130: host 192.168.3.100 is not allowed to connect to this MySQL s

Kubernetes详解(四)——基于kubeadm的Kubernetes部署

最基本的JDBC模板及数据库乱码处理

Appreciation of single chip microcomputer DIY works, beginners come in to worship

What can MCU do? Do you have any interesting works made by MCU or open source hardware

【论文精读】Stable Linear Structures and Seam Measurements for Parallax Image Stitching

Laravel soar (2. X) - automatically monitor and output SQL optimization suggestions and assist laravel to apply SQL optimization

华为、TCL、大疆

Headline we media operation secret script. If you stick to it, you can beat 90% of the people

【网络】4G、5G频段汇总
随机推荐
laravel-soar(2.x) - 自动监控输出 SQL 优化建议、辅助 laravel 应用 SQL 优化
Logstash ~ output of logstash
C# operator
Intermediary model
Packaging
Variable与Tensor合并后,关于训练、验证的相关变化
Integer面试解析、equals与==的区别
MySQL query table field default value
C语言进阶第46式:函数与宏分析
终于有人讲明白了!原来这才是全球低时延一张网技术
【NPJ|数字医药】医学影像的机器学习:方法学的失败和对未来的建议
Interface
SQL command delete (I)
"Industrial Internet plus safety production" to enhance the safety level of industrial enterprises
Q: Making line chart with Excel
还剩下载次数,可留链接+邮箱
[刷力扣] 51-60题
爬虫案例01
一个通用的CommonDialog
About LINQ statements