Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use serialization and deserialization of Json in Golang

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use the serialization and deserialization of Json in Golang". In daily operation, I believe that many people have doubts about the serialization and deserialization of Json in Golang. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to use the serialization and deserialization of Json in Golang". Next, please follow the editor to study!

JSON:

JSON (JavaScript Object Notation): is a lightweight data exchange format. It is a subset of the ECMAScript specification that stores and represents data in a text format that is completely independent of the programming language.

The concise and clear hierarchical structure makes JSON an ideal data exchange language. Easy for people to read and write, but also easy for computer analysis and generation, and effectively improve the efficiency of network transmission.

Json is easy to be parsed and generated by machines, and effectively improves the efficiency of network transmission. Usually, when a program transmits, it serializes the data into a json string first, and then the receiver deserializes it into the original data type.

All data types can be represented by Json

Json.cn is a website that can verify Json format.

Json.Marshal () for serialization

Deserialization uses json.Unmarshal (). Deserialization should be consistent with the data type before serialization.

Create a 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}

Structural body serialization:

Func main () {testStudent ()} / * * type Student struct {Name string Age int Birthday string Address string} * / / if `json: "student_name" `is added, the serialized data fields are returned in the specified format, which can be lowercase and json fixed. The following random type Student struct {/ / variables can only be parsed with uppercase initials 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: "Guangzhou Tianhe District",} 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": "Guangzhou Tianhe District"}} map serialization: func testMap () {var address [string] interface {} m = make (map [string] interface {}) m ["name"] = "Han Xin" m ["age"] = 23m ["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 map [string] interface {} m = make (map [string] interface {}) m ["name"] = "Han Xin" m ["age"] = 23m ["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 into structure: func deserialize () {str: = "{\" Name\ ":\" itzhuzhu\ ",\" Age\ ": 24,\" Birthday\ ":\" 1998-01-01\ ",\" Address\ ":\" Tianhe District, Guangzhou\ "}" / / use Unmarshal to deserialize 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 Guangzhou Tianhe District} is deserialized to map:func deserializeMap () {str: = "{\" address\ ":\" Guangzhou\ ",\" age\ ": 23 \ "name\":\ "Han Xin\"} "/ / No make is required for deserialization Encapsulated in Unmarshal var mmap[ 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 [] 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]]} so far The study on "how to serialize and deserialize Json in Golang" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report