Created
January 14, 2020 13:35
-
-
Save uurtech/a3c1489c88adf53abb558e905bf1cd08 to your computer and use it in GitHub Desktop.
JSON unmarshalJson Timestamp to ISO Date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"strconv" | |
"time" | |
) | |
type CustomTime struct { | |
Time time.Time | |
} | |
const ctLayout = "2006-01-02T15:04:05Z0700" | |
func (ct *CustomTime) UnmarshalJSON(b []byte) (err error) { | |
s := string(b) | |
i, err := strconv.ParseInt(s, 10, 64) | |
if err != nil { | |
panic(err) | |
} | |
tm := time.Unix(i, 0) | |
ct.Time = tm | |
return nil | |
} | |
type User struct { | |
Time CustomTime `json:"Time"` | |
} | |
func main() { | |
var b = User{} | |
var data = `{"Time":1579008351}` | |
json.Unmarshal([]byte(data), &b) | |
fmt.Printf("%v", b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment