dongyi9330 2019-02-10 08:42 采纳率: 100%
浏览 2442
已采纳

将时间“””解析为“ 2006-01-02T15:04:05Z07:00”:无法将“”解析为“ 2006”

我正在尝试将一些json解组到结构中,出现以下内容:

package main

import (
    "encoding/json"
    "fmt"
    "strings"
    "time"
)

type Added struct {
    Added *time.Time `json:"added"`
}

func main() {
    st := strings.NewReader(`{"added": ""}`)

    a := &Added{}
    err := json.NewDecoder(st).Decode(&a)
    if err != nil {
        panic(err)
    }

    fmt.Println(a)

}

运行以上结果将导致:

panic: parsing time """" as ""2006-01-02T15:04:05Z07:00"": cannot parse """ as "2006"

于是我尝试一个自定义编组器:

package main

import (
    "encoding/json"
    "fmt"
    "strings"
    "time"
)

type Added struct {
    Added *MyTime `json:"added"`
}

func main() {
    st := strings.NewReader(`{"added": ""}`)

    a := &Added{}
    err := json.NewDecoder(st).Decode(&a)
    if err != nil {
        panic(err)
    }

    fmt.Println(a)

}

type MyTime struct {
    *time.Time
}

func (m *MyTime) UnmarshalJSON(data []byte) error {
    // Ignore null, like in the main JSON package.
    if string(data) == "null" || string(data) == `""` {
        return nil
    }
    // Fractional seconds are handled implicitly by Parse.
    tt, err := time.Parse(`"`+time.RFC3339+`"`, string(data))
    *m = MyTime{&tt}
    return err
}

然后我得到:

&{%!v(PANIC=runtime error: invalid memory address or nil pointer dereference)}

那现在我该怎么办? 我只想处理json中的“”值.

并能够找到带有完整示例的游乐场。

展开全部

  • 写回答

2条回答 默认 最新

  • dsgk0386 2019-02-10 08:57
    关注

    Package time

    import "time"

    type Time

    A Time represents an instant in time with nanosecond precision.

    Programs using times should typically store and pass them as values, not pointers. That is, time variables and struct fields should be of type time.Time, not *time.Time.


    I just kept fixing likely problems, for example, time.Time, not *time.Time, a real date, and so on, until I got a reasonable result:

    package main
    
    import (
        "encoding/json"
        "fmt"
        "strings"
        "time"
    )
    
    type MyTime struct {
        time.Time
    }
    
    func (m *MyTime) UnmarshalJSON(data []byte) error {
        // Ignore null, like in the main JSON package.
        if string(data) == "null" || string(data) == `""` {
            return nil
        }
        // Fractional seconds are handled implicitly by Parse.
        tt, err := time.Parse(`"`+time.RFC3339+`"`, string(data))
        *m = MyTime{tt}
        return err
    }
    
    type Added struct {
        Added MyTime `json:"added"`
    }
    
    func main() {
        st := strings.NewReader(`{"added": "2012-04-23T18:25:43.511Z"}`)
    
        var a Added
        err := json.NewDecoder(st).Decode(&a)
        if err != nil {
            panic(err)
        }
        fmt.Println(a)
    }
    

    Playground: https://play.golang.org/p/Uusdp3DkXDU

    Output:

    {2012-04-23 18:25:43.511 +0000 UTC}
    

    With an empty ("") date string, the time.Time zero value, 0001-01-01 00:00:00 +0000 UTC:

    Playground: https://play.golang.org/p/eQoEyqBlhg2

    Output:

    {0001-01-01 00:00:00 +0000 UTC}
    

    Use the time IsZero method to test for the zero value.

    func (Time) IsZero

    func (t Time) IsZero() bool
    

    IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 有人会fastcrud写前端页面吗
  • ¥15 如何解除Uniaccess管控
  • ¥15 微信小程序跳转关联公众号
  • ¥15 Java AES 算法 加密采用24位向量报错如何处理?
  • ¥15 使用X11可以找到托盘句柄,监控到窗口点击事件但是如何在监听的同时获取托盘中应用的上下文菜单句柄
  • ¥45 字符串操作——数组越界问题
  • ¥15 Loss下降到0.08时不在下降调整学习率也没用
  • ¥15 QT+FFmpeg使用GPU加速解码
  • ¥15 为什么投影机用酷喵播放电影放一段时间就播放不下去了?提示发生未知故障,有什么解决办法吗?
  • ¥15 来个会搭建付费网站的有偿