取消封送JSON [重复]时的零日期

问题描述 投票:0回答:1

我正在尝试使用时间值解组JSON。我有这个JSON结构。

{
        "Nick": "cub",
        "Email": "[email protected]",
        "Created_at": "2017-10-09",
        "Subscribers": [
            {
                "Email": "[email protected]",
                "Created_at": "2017-11-30"
            },
            {
                "Email": "[email protected]",
                "Created_at": "2016-07-15"
            },
            {
                "Email": "[email protected]",
                "Created_at": "2017-02-16"
            },
            {
                "Email": "[email protected]",
                "Created_at": "2016-11-30"
            },
            {
                "Email": "[email protected]",
                "Created_at": "2018-07-10"
            },
            {
                "Email": "[email protected]",
                "Created_at": "2017-09-04"
            },
            {
                "Email": "[email protected]",
                "Created_at": "2018-01-03"
            },
            {
                "Email": "[email protected]",
                "Created_at": "2018-04-20"
            },
            {
                "Email": "[email protected]",
                "Created_at": "2018-02-17"
            }
        ]
    }

和一个实现从JSON文件读取以构造用户的函数。一切正常,但是当我在User struct中将CreatedAt字段设置为time.Time类型时,对于JSON文件中具有类型格式的每个字段,我得到0001-01-01 00:00:00 +0000 UTC值。

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)
    }

}

以RFC3339格式从JSON文件向用户读取时间的合适方法是什么?

json go time unmarshalling
1个回答
1
投票
您可以使用实现json.Unmarshaler界面的自定义时间类型。

您可以从此结构开始:

type CustomTime struct { time.Time // Embed time.Time to allow calling of normal time.Time methods }

然后添加所需的UnmarshalJSON([]byte) error功能。它可能看起来像这样:

func (c *CustomTime) UnmarshalJSON(b []byte) error { if len(b) < 3 { // Empty string: "" return fmt.Errorf("Empty time value") } t, err := time.Parse("2006-01-02", string(b[1:len(b)-1])) // b[1:len(b)-1] removes the first and last character, as they are quotes if err != nil { return err } c.Time = t return nil }

您可以在Go Playground上尝试该示例
© www.soinside.com 2019 - 2024. All rights reserved.