I'm trying to unmarshal JSON with time value. I have this JSON structure.
{
"Nick": "cub",
"Email": "cub875@rambler.ru",
"Created_at": "2017-10-09",
"Subscribers": [
{
"Email": "rat1011@rambler.ru",
"Created_at": "2017-11-30"
},
{
"Email": "hound939@rambler.ru",
"Created_at": "2016-07-15"
},
{
"Email": "worm542@rambler.ru",
"Created_at": "2017-02-16"
},
{
"Email": "molly1122@rambler.ru",
"Created_at": "2016-11-30"
},
{
"Email": "goat1900@yandex.ru",
"Created_at": "2018-07-10"
},
{
"Email": "duck1146@rambler.ru",
"Created_at": "2017-09-04"
},
{
"Email": "eagle1550@mail.ru",
"Created_at": "2018-01-03"
},
{
"Email": "prawn1755@rambler.ru",
"Created_at": "2018-04-20"
},
{
"Email": "platypus64@yandex.ru",
"Created_at": "2018-02-17"
}
]
}
And a function that implements reading from JSON file to struct User. Everything if fine but when I set CreatedAt field in User struct to time.Time type I get 0001-01-01 00:00:00 +0000 UTC value for each field with type format in JSON file.
type Subscriber struct {
Email string `json: "Email"`
CreatedAt time.Time `json: "Created_at"`
}
type User struct {
Nick string `json: "Nick"`
Email string `json: "Email"`
CreatedAt string `json: "Created_at"`
Subscribers []Subscriber `json: "Subscribers"`
}
func main() {
// Slice where objects from users.json will be pushed
var jsonData []User
// ReadJSON and push User struct to jsonData slice
readJSON("users.json", &jsonData)
for _, user := range jsonData {
fmt.Println(user.Nick, user.Email, user.CreatedAt) // 0001-01-01 00:00:00 +0000 UTC
fmt.Println(user.Subscribers)
}
}
func readJSON(fileName string, userSlice *[]User) {
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
bytes, err := ioutil.ReadAll(file)
if err != nil {
log.Fatal(err)
}
err1 := json.Unmarshal(bytes, &userSlice)
if err1 != nil {
log.Fatal(err1)
}
}
What is an appropriate way to read time from JSON file to User in RFC3339 format?