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 implement JSON parsing in Go language

2025-01-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 achieve JSON parsing in Go language". In daily operation, I believe many people have doubts about how to achieve JSON parsing in Go language. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to achieve JSON parsing in Go language". Next, please follow the editor to study!

1. Json serialization

Converts a json string to a go language structure object.

Package main import ("encoding/json"errors"fmt") var parseJsonError = errors.New ("json parse error") var toJsonError = errors.New ("to json error") type CustomJson struct {Name string Age int} / / serialized to jsonfunc toJson (c * CustomJson) (string, error) {fmt.Printf ("original structure:% v\ n", c) if jsonStr, err: = json.Marshal (c) Err! = nil {fmt.Println ("Error =", err) return ", parseJsonError} else {return string (jsonStr), nil}} func main () {w: = CustomJson {Name:" Li Si ", Age: 30} result, _: = toJson (& w) fmt.Println (result)}

Execution result

Original structure: & {Li Si 30}

{"Name": "Li Si", "Age": 30}

Be careful

The first letter of the field of the structure should be capitalized. What if the first letter of the json string is lowercase? I'll talk about the use of tag later.

2. Json is deserialized to structure object.

Deserializes an json string into a structure object.

The sample code is as follows

Package main import ("encoding/json"errors"fmt") var parseJsonError = errors.New ("json parse error") var toJsonError = errors.New ("to json error") type CustomJson struct {Name string Age int} / / deserialized into structural object func parseJson (a string) (CustomJson, error) {fmt.Printf ("original string:% s\ n", a) var c CustomJson if err: = json.Unmarshal ([] byte (a) & c) Err! = nil {fmt.Println ("Error =", err) return c, parseJsonError} return c, nil} func main () {j: = `{"Name": "Zhang San", "Age": 19} `if p, e: = parseJson (j); e = = nil {fmt.Printf ("conversion object:% v\ n", p)}}

Execution result

Original string: {"Name": "Zhang San", "Age": 19}

The conversion object is: {Zhang San 19}

3. Json is deserialized to map type

Deserialize json strings to map types

The sample code is as follows

Package main import ("encoding/json"errors"fmt") var parseJsonError = errors.New ("json parse error") var toJsonError = errors.New ("to json error") type CustomJson struct {Name string Age int} / / deserialized to mapfunc parseMap (a string) (map [string] interface {}, error) {fmt.Printf ("original string:% s\ n") A) var mmap [string] interface {} if err: = json.Unmarshal ([] byte (a), & m) Err! = nil {fmt.Println ("Error =", err) return m, parseJsonError} for k, v: = range m {fmt.Printf, e: = parseMap (j)} `if m, e: = parseMap (j) E = = nil {fmt.Printf ("convert map to:% v\ n", m)}}

Execution result

Original string: {"Name": "Zhang San", "Age": 19}

The v type of karmename is string,v= Zhang San

The type of kryptonagecore v is float64,v=19.

Convert map to: map [Age:19 Name: Zhang San]

Be careful

Although it is of type int, it defaults to float64 if no conversion is made during deserialization.

4. The use of Tag

If the first letter of each key in the resulting json string is lowercase, how can it be converted to a go structure object? You can use the tag method.

The sample code is as follows

Package main import ("encoding/json"fmt") type TestJson struct {Name string `json: "name" `Age int `json: "age" `} func main () {j: =` {"name": "Zhang San", "age": 19} `var c TestJson if err: = json.Unmarshal ([] byte (j), & c) Err! = nil {fmt.Println ("Error =", err)} fmt.Println (">", c)}

Execution result

> {Zhang San 19}

Be careful

Field conversion can be achieved by calling the key of the original string as a tag to the field of the structure.

At this point, the study of "how to implement JSON parsing in Go language" 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