json 中的时间转 struct 问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
hunk

json 中的时间转 struct 问题

  •  
  •   hunk Aug 16, 2021 1409 views
    This topic created in 1729 days ago, the information mentioned may be changed or developed.
    问题 1:
    json 中的时间格式不标准"create_time":"2021-08-14 11:36:37"
    在转为 struct 时,不能直接转为 time.Time 。
    网上找了别人写好的 UnmarshalJSON 方法,依然不能解析。

    问题 2:
    将 struct 改为 string,输出是空。
    在 go 中,json.Unmarshal 解析前的代码是 bytes 数组。手动执行 curl,得到的结果是有的。
    wfhtqp
        1
    wfhtqp  
       Aug 16, 2021
    自定义类型并实现 Unmarshal/Marshal 方法
    danc
        2
    danc  
       Aug 17, 2021
    ```go
    package util

    import "time"

    func ParseTime(value string) (time.Time, error) {
    loc := time.FixedZone("UTC+8", +8*60*60)

    return time.ParseInLocation("2006-01-02 15:04:05", value, loc)
    }

    func TimeFormat(t time.Time) string {
    loc := time.FixedZone("UTC+8", +8*60*60)
    return t.In(loc).Format("2006-01-02 15:04:05")
    }

    type MyTime time.Time

    func (myT MyTime) MarshalText() (data []byte, err error) {
    t := time.Time(myT)
    data = []byte(TimeFormat(t))
    return
    }

    func (myT *MyTime) UnmarshalText(text []byte) (err error) {
    t := (*time.Time)(myT)
    *t, err = ParseTime(string(text))
    return
    }
    ```

    然后

    ```go
    type MonitorDiagnosisItem struct {
    Time util.MyTime `json:"time"`
    }
    ```

    学会了吗?
    hunk
        3
    hunk  
    OP
       Aug 17, 2021
    好详细,谢谢。我用了两个 struct 做转换,回头试试这种写法
    About     Help     Advertise     Blog     API     FAQ     Solana     5783 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 74ms UTC 06:01 PVG 14:01 LAX 23:01 JFK 02:01
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86