Time format solution for Golang structure JSON

Source: Internet
Author: User
Tags sprintf

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 the addition of Marshaljson, Unmarshaljson, String method to any struct, implements the custom JSON output format and printing way.

This article is from the "dream to think XI" blog, please be sure to keep this source http://qiangmzsx.blog.51cto.com/2052549/1947844

Time format solution for Golang structure JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.