A recent development project found a time format problem for JSON conversion of a struct.
That is, this 1993-01-01t20:08:23.000000028+08:00 represents the UTC method. From what we used to say, it's more like hope.
1993-01-01 20:08:23 this format.
The re-replication code is as follows:
Package Mainimport ("Time" "Encoding/json") type Student struct {name string ' JSON: ' name ' ' Brith time. Time ' JSON: "Brith" '}func Main () {stu:=student{Name: "Qiangmzsx", Brith:time. Date (1993, 1, 1, 8,. Local),} b,err:=json. Marshal (stu) if Err!=nil {println (Err)} println (string (b))//{"name": "Qiangmzsx", "Brith": "1993-01-01t20:0 8:23.000000028+08:00 "}}
In the face of such problems, how is Golang solved?
There are two kinds of solutions, let's take a look at each one.
pass time. Time Type alias
Type jsontime time. time// implements its JSON serialization method func (This jsontime) marshaljson () ([]byte, error) { var stamp = fmt. Sprintf ("\"%s\ "", time. Time (this). Format ("2006-01-02 15:04:05")) return []byte (stamp), nil}type student1 struct { name string ' JSON: ' Name ' ' Brith JsonTime ' JSON: "Brith" '}func main () { stu1:=student1{ name: "Qiangmzsx", brith:jsontime (time. Date (1993, 1, 1, 20, 8, 23, 28, time. Local)), } b1,err:=json. Marshal (STU1) if err!=nil { println (ERR) &nbsP;} println (String (B1))//{"name": "Qiangmzsx", "Brith": "1993-01-01 20:08:23"}}
Using struct-body combination methods
It's a bit more complicated than the first, and I'm not recommending it as an extended tutorial.
type student2 struct { name string ' JSON: "Name" // be sure to ignore the JSON tag settings and not parse out the brith time. time ' JSON: '-' '}// implements its JSON serialization method func (This student2) marshaljson () ([]byte , error) { // defines an alias for the struct     TYPE ALIASSTU Student2 // define a new structural body tmpstudent:= struct { aliasstu brith string ' JSON: ' Brith ' }{ Aliasstu: (Aliasstu) (this), brith:this. Brith.format ("2006-01-02 15:04:05"), } return json. Marshal (tmpstudent)}func main () { stu2:=student2{ name: "Qiangmzsx", brith:time. Date (1993, 1, 1, 20, 8, 23, 28, time. Local), } b2,err:=json. Marshal (STU2) if err!=nil { println (Err) } println (String (b2))//{"name": "Qiangmzsx", "Brith": "1993-01-01 20:08:23"}}
This method uses the combination of Golang structure, can realize the inheritance of OOP, but also embodies golang flexibility.
Below, the above code is composed of the whole.
package mainimport ( "Time" "Encoding/json" //"FMT" "FMT") type student struct { Name string ' JSON: ' Name ' ' brith time. time ' JSON: "Brith" '}type jsontime time. time// implements its JSON serialization method func (This jsontime) marshaljson () ([]byte, error) { var stamp = fmt. Sprintf ("\"%s\ "", time. Time (this). Format ("2006-01-02 15:04:05")) return []byte (stamp), nil}type student1 struct { name string ' JSON: ' Name ' ' Brith JsonTime ' JSON: "Brith" '}type student2 struct { Name string ' JSON: ' Name ' '    &NBsp;// Be sure to ignore the JSON tag settings do not parse out the brith time. time ' JSON: '-' '}// implements its JSON serialization method func (This student2) marshaljson () ([]byte , error) { // defines an alias for the struct     TYPE ALIASSTU Student2 // define a new structural body tmpstudent:= struct { aliasstu brith string ' JSON: ' Brith ' }{ Aliasstu: (Aliasstu) (this), brith:this. Brith.format ("2006-01-02 15:04:05"), } return json. Marshal (tmpstudent)}func main () { stu:=Student{ name: "Qiangmzsx",        &NBsp Brith:time. Date (1993, 1, 1, 20, 8, 23, 28, time. Local), } b,err:=json. Marshal (Stu) if err!=nil { println (Err) } println (string (b))//{"name": "Qiangmzsx", "Brith": " 1993-01-01t20:08:23.000000028+08:00 "} println (" =================== ") stu1:=student1{ name: "Qiangmzsx", brith:jsontime (time. Date (1993, 1, 1, 20, 8, 23, 28, time. Local)), } b1,err:=json. Marshal (STU1) if err!=nil { println (Err) } println (String (B1))//{"name": "Qiangmzsx", "Brith": "1993-01-01 20:08:23 "} println (" =================== ") stu2:= Student2{ name: "Qiangmzsx", brith:time. Date (1993, 1, 1, 20, 8, 23, 28, time. Local), } b2,err:=json. Marshal (STU2) if err!=nil { println (Err) } println (String (b2))//{"name": "Qiangmzsx", "Brith": "1993-01-01 20:08:23"}}
It is worth mentioning that add marshaljson ,unmarshaljson, string method to any struct, implement custom JSON output format and printing way.