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

What is the principle of go json parser

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is the principle of go json parser". In daily operation, I believe that many people have doubts about the principle of go json parser. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the principle of go json parser?" Next, please follow the editor to study!

Selection of Serialization Library

When writing a small demo or making a gadget, without large-scale usage scenarios, either library will be the same, because the performance will not be obvious. However, if it is used in a real project, and accompanied by high concurrency, high capacity and other scenarios, I still recommend using json-iterator.

Go comes with json library "encoding/json" official with json-iterator

Claims to be the fastest go json parser. Compatible with the official way of writing, I basically use this at present.

Efficiency comparison

Ns nanosecond op operation

Ns/opallocation bytesallocation timesstd decode35510 ns/op1960 B/op99 allocs/opeasyjson decode8499 ns/op160 B/op4 allocs/opjsoniter decode5623 ns/op160 B/op3 allocs/opstd encode2213 ns/op712 B/op5 allocs/opeasyjson encode883 ns/op576 B/op3 allocs/opjsoniter encode837 ns/op384 B/op4 allocs/op coding case type Hero struct {Name string Age int Birthday string Sal float64 Skill string} Serialization hero: = Hero {Name: "Xiao Wang" Age: 20, Birthday: "2021-02-23", Sal: 88.02, Skill: "skills",} jsonStu, err: = json.Marshalif err! = nil {fmt.Println ("generate json words} fmt.Println (string (jsonStu) deserialization structure structstr: =" {\ "Name\":\ "Zhang Sanfeng\",\ "Age\": 98 \ "Birthday\":\ "2001-09-21\",\ "Sal\": 3800.85,\ "Skill\":\ "Wudang swordsmanship\"} "var hero Heroerr: = json.Unmarshal ([] byte (str), & hero) if err! = nil {fmt.Printf (" unmarshal err=%v\ n ", err)} structure array

There are two ways, one is directly deserialized into a structure array, and the other is deserialized to slice with the content of map [string] interface {}

Structure array

Str: = `[{"Name": "Zhang Sanfeng", "Age": 98, "Birthday": "2001-09-21", "Sal": 3800.85, "Skill": "martial arts"}, {"Name": "Zhang Wuji", "Age": 28, "Birthday": "2004-09-21", "Sal": 300.85, "Skill": "Great shift of the universe"}] `var hero [] Heroerr: = json.Unmarshal ([] byte (str)) & hero) if err! = nil {fmt.Printf ("unmarshal err=%v\ n", err)} fmt.Printf ("deserialized hero=%v", hero)

Slice

Str: = `[{"Name": "Zhang Sanfeng", "Age": 98, "Birthday": "2001-09-21", "Sal": 3800.85, "Skill": "martial arts"}, {"Name": "Zhang Wuji", "Age": 28, "Birthday": "2004-09-21", "Sal": 300.85 "Skill": "Great shift of the universe"}] `/ / define a slicevar slice [] map [string] interface {} / / Note: deserialize map, no make is required Because the make operation is encapsulated into the Unmarshal function err: = json.Unmarshal ([] byte (str), & slice) if err! = nil {fmt.Printf ("unmarshal err=%v\ n", err)} fmt.Printf ("deserialized slice=%v\ n", slice) so far, the study of "what is the principle of the go json parser" 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