package main
import (
"encoding/json"
"fmt"
"time"
)
func main() {
eur, err := time.LoadLocation("Europe/Vienna")
if err != nil {
panic(err)
}
t := time.Date(2017, 11, 20, 11, 20, 10, 0, eur)
// json.Marshaler interface
b, err := t.MarshalJSON()
if err != nil {
panic(err)
}
fmt.Println("Serialized as RFC 3339:", string(b))
t2 := time.Time{}
t2.UnmarshalJSON(b)
fmt.Println("Deserialized from RFC 3339:", t2)
// Serialize as epoch
epoch := t.Unix()
fmt.Println("Serialized as Epoch:", epoch)
// Deserialize epoch
jsonStr := fmt.Sprintf("{ \"created\":%d }", epoch)
data := struct {
Created int64 `json:"created"`
}{}
json.Unmarshal([]byte(jsonStr), &data)
deserialized := time.Unix(data.Created, 0)
fmt.Println("Deserialized from Epoch:", deserialized)
}
/*
Serialized as RFC 3339: "2017-11-20T11:20:10+01:00"
Deserialized from RFC 3339: 2017-11-20 11:20:10 +0100 +0100
Serialized as Epoch: 1511173210
Deserialized from Epoch: 2017-11-20 18:20:10 +0800 CST
*/
4.12 序列化
Posted cucy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4.12 序列化相关的知识,希望对你有一定的参考价值。
以上是关于4.12 序列化的主要内容,如果未能解决你的问题,请参考以下文章
代码:程序清单4.12_floatcnv.c程序_《C Primer Plus》P75