gpt4 book ai didi

Json Decode 无法将 json 中的时间戳解析为 Go 结构

转载 作者:IT王子 更新时间:2023-10-29 01:33:47 26 4
gpt4 key购买 nike

我正在尝试获取一个 HTTP 请求正文,它是一个 json 对象并将其解码为我定义的 Go 结构。

该结构的两个字段是time.Time 类型。虽然只有一个这样的类型字段,但一切正常。

如果我在 go 结构中有多个 time.Time 类型的字段,我无法解码它并得到错误:

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

问题出在解码线路上。尽管我进行了调试工作,但我本可以得出有意义的结果。这个问题看起来很奇怪,实际上不应该。

我在这里错过了什么?

func register(w http.ResponseWriter, r *http.Request){
//Read Request Body JSON Into Go Types

var requestBody = []byte(`{"username":"qwewwwqweqwe","password":"can","usertype":"student","firstname":"",‌​"midname":null,"surname":null,"signuptimestamp":null,"userstatus":null,"phone":nu‌​ll,"email":null,"address":null,"city":null,"country":null,"language":null,"lastlo‌​gintimestamp":null}`)

type RegisterStructure struct {
Id int `json:"id"`
Timestamp time.Time `json:"timestamp,omitemty"`
SignupTimestamp time.Time `json:"signuptimestamp,omitempty"`
Username string `json:"username"`
Password string `json:"password"`
UserType string `json:"usertype"`
FirstName string `json:"firstname"`
Midname string `json:"midname"`
Surname string `json:"surname"`
UserStatus string `json:"userstatus"`
Phone string `json:"phone"`
Email string `json:"email"`
Address string `json:"address"`
City string `json:"city"`
Country string `json:"country"`
Language string `json:"language"`
//LastLoginTimestamp time.Time `json:"lastlogintimestamp,omitempty"`
}
var registerInstance RegisterStructure
var now = time.Now()
fmt.Printf("now is %v", now)
fmt.Println()
fmt.Printf("1 registerInstance after inited here is %v", registerInstance)
fmt.Println()

registerInstance = RegisterStructure{Timestamp: now, SignupTimestamp: now,}
fmt.Printf("registerInstance after set to var now here is %v", registerInstance)
fmt.Println()

dec := json.NewDecoder(bytes.NewReader(requestBody))
err = dec.Decode(&registerInstance)
if err != nil {
fmt.Printf("error happens here.")
log.Fatal(err)
}

最佳答案

好的。这是一个reproducible example这说明了您所看到的错误。

package main

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

type RegisterStructure struct {
SignupTimestamp time.Time `json:"signuptimestamp,omitempty"`
}

func main() {
requestBody := []byte(`{"signuptimestamp" : null}`)
dec := json.NewDecoder(bytes.NewReader(requestBody))
registerInstance := RegisterStructure{}
err := dec.Decode(&registerInstance)
if err != nil {
fmt.Println(err)
}
}

您看到的错误与具有多个时间戳无关。这就是为什么显示输入对于像这样的调试情况至关重要,以及为什么您应该返回并更改您的问题以包含示例 requestBody 作为问题内容的一部分。否则,将变得非常难以猜出您在做什么。

发生的事情是 null 没有被 time.Time 的 JSON 解码器处理。 documentation对于 time.Time 的 unmarshaller 说:UnmarshalJSON 实现了 json.Unmarshaler 接口(interface)。时间应为 RFC 3339 格式的带引号的字符串。

null 不是这样的值:这种情况下的解码器不会尝试为时间戳构建“零”值。

您要做的是将 SignupTimestamp 的类型从 time.Time 更改为 *time.Time,以便 null 值可以显式表示为 nil 指针。

这是一个 example演示:

package main

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

type RegisterStructure struct {
SignupTimestamp *time.Time `json:"signuptimestamp,omitempty"`
}

func main() {
requestBodies := []string{
`{"signuptimestamp" : "1985-04-12T23:20:50.52Z"}`,
`{"signuptimestamp" : null}`,
}
for _, requestBody := range requestBodies {
dec := json.NewDecoder(bytes.NewReader([]byte(requestBody)))
registerInstance := RegisterStructure{}
err := dec.Decode(&registerInstance)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%#v\n", registerInstance)
}
}

关于Json Decode 无法将 json 中的时间戳解析为 Go 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26684752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com