Skip to content

Instantly share code, notes, and snippets.

@uurtech uurtech/main.go
Created Jan 14, 2020

Embed
What would you like to do?
JSON unmarshalJson Timestamp to ISO Date
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
You can’t perform that action at this time.