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 tag when parsing JSON in Go

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

Share

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

This article mainly introduces the relevant knowledge of "how to use tag when parsing JSON in Go". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to use tag when parsing JSON in Go" can help you solve the problem.

When dealing with json-formatted strings, you will often see backquotation marks on the right side of the attribute when declaring the struct structure. It looks like:

Type User struct {UserId int `json: "user_id" bson: "user_id" `UserName string `json: "user_name" bson: "user_name" `} struct member variable tag (Tag) description

To understand this in more detail, you should first understand the basis of golang. In golang, naming is recommended in hump mode, and there is a special grammatical meaning in the case of the first letter: it cannot be referenced outside the package. However, it is often necessary to exchange data with other systems, such as converting to json format, storing to mongodb, and so on. At this time, if you use the attribute name as the key value, it may not necessarily meet the project requirements.

So, there are more backquotes, which are called Tag in golang, and when converted to other data formats, specific fields are used as key values. For example, the above example is converted to json format:

U: = & User {UserId: 1, UserName: "tony"} j, _: = json.Marshal (u) fmt.Println (string (j)) / / output: / / {"user_id": 1, "user_name": "tony"} / / if there is no tag description in the attribute, then output: / / {"UserId": 1, "UserName": "tony"} / / you can see the key value directly using the attribute name of struct. / / = = there is also a declaration of bson, which is used to get the = = struct member variable tag (Tag) used to store data in mongodb

So how do we get it when we need to encapsulate some operations and use the content in Tag? Here, you can use the method in the reflection package (reflect) to obtain: the complete code is as follows:

Package mainimport ("encoding/json"fmt"reflect") func main () {type User struct {UserId int `json: "user_id" bson: "user_id" `UserName string `json: "user_name" bson: "user_name" `} / / output json format u: = & User {UserId: 1, UserName: "tony"} j _: = json.Marshal (u) fmt.Println (string (j)) / / output: {"user_id": 1 "user_name": "tony"} / / get the content in tag t: = reflect.TypeOf (u) field: = t.Elem () .Field (0) fmt.Println (field.Tag.Get ("json")) / / output: user_id fmt.Println (field.Tag.Get ("bson")) / / output: user_id} Custom tag type User struct {UserId int `json: "user_id" bson: "user_id" test: "test" `UserName string `json: "user_name" bson: "user_name" `}

Get the value of test in tag

/ / get the contents of tag typeof: = reflect.TypeOf (u) field: = typeof.Elem (). Field (0) fmt.Println (field.Tag.Get ("json")) / / output: user_idfmt.Println (field.Tag.Get ("bson")) / / output: user_idfmt.Println (field.Tag.Get ("test")) / / output: test's introduction to "how to use tag in JSON parsing in Go" ends here Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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