This is a creation in Article, where the information may have evolved or changed.
I. Introduction of Structtag
Structtag is a marker field in the Go language structure, which is placed behind the field declaration and is typically used only in certain situations, such as JSON conversions.
As long as you use the Marked field with two 包起来即可,标签内的语法形式通常为
key: "Value", multiple markers are separated by a space.
1 2 3 4
|
type struct { string ' Taga ' int ' TAGB ' }
|
Second, the struct to JSON
To convert a struct to JSON, simply set the field name in the structure to the export state, i.e. capitalize the first letter, and by default the key value in the converted JSON is the same as the field name in the struct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23
|
Package main Import ( "FMT" "Encoding/json" ) type addr struct { province String City String } type stu struct { Name string Age int Addr Addr } func main(){ var xm = stu{name:"Xiaoming", Age:addr:addr{province:"Hunan", City: "Changsha"}} js, err: = json. Marshal (XM) if err! = Nil{ FMT. PRINTLN (ERR) return } FMT. Println (string(JS)) //output {"Name": "Xiaoming", "Age": $, "Addr": {"Province": "Hunan", "City": "Changsha"}} }
|
Third, JSON to the struct
When converting JSON into a struct, it is necessary for JSON and struct fields to be consistent to be successfully converted, otherwise the transformed data may be different from what we think.
Note that the case can be ignored here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24
|
Package main Import ( "FMT" "Encoding/json" ) type addr struct { province String City String } type stu struct { Name string Age int Addr Addr } func main(){ js: = ' {' Age ': ', ' ' name ': ' Xiaoming ', ' Addr ': {' Province ': ' Hunan ', ' City ': ' Changsha '} ' //name ' is lowercase var xm Stu ERR: = json. Unmarshal ([]byte(JS), &XM) if err! = Nil{ FMT. PRINTLN (ERR) return } FMT. Println (XM) //output {xiaoming {hunan Changsha} } }
|
Iv. Use of Tag
None of the above tags can be converted to each other, it seems that tag does not have any use! This is indeed the case, tag is not really necessary here.
But if we expect the converted JSON field names to be different from the struct, we have to use tag.
The purpose of tag here is to provide aliases to make the conversion more flexible.
The use of the tag format is json:"***"
, ' (* * * We expect the JSON after the conversion of the name)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26
|
Package main Import ( "FMT" "Encoding/json" ) type addr struct { province string ' json: ' Province ' //conversion of the corresponding field JSON named Province City string ' JSON: ' city ' } type stu struct { name string ' JSON: ' Name ' Age int ' JSON: ' age ' Addr Addr ' JSON: "Addr" } func main(){ var xm = stu{name: "Xiaoming", Age: addr:addr{province:"Hunan", City:"Changsha"}} js, err: = json. Marshal (XM) if err! = Nil{ FMT. PRINTLN (ERR) return } FMT. Println (string(JS)) var xxm Stu err = json. Unmarshal (JS, & XXM) FMT. Println (XXM) }
|
Output:
1 2 |
{ "name" : "Xiaoming" , : 18 , : {: "Hunan ", : " Changsha "}}// The conversion will follow the name we specify. {xiaoming 18 {hunan changsha}}< span class= "comment" >//and vice versa |