为什么 JSON 绑定中的时间格式会发生变化?
why format of time changes in JSON binding?
我创建了一个结构,它包含两个 time.Time 格式的字段,用 json 标签命名:start_time和end_time.
type MyStruct struct {
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
}
当我尝试使用 gin 框架通过 HTTP 发送 PUT 请求以更新这些值时,我发送的时间格式改变了发送结构。
我要发送的内容:
curl -X PUT -H 'Content-Type: application/json'
http://my_address -d '{
"start_time": "2021-04-27T22:24:31Z",
"end_time": "2021-11-01T22:24:31Z"
}'
它收到什么:
start_time="2021-04-27 22:24:31 +0000 UTC",
end_time="2021-11-01 22:24:31 +0000 UTC",
另一方面,
我将结构保存在 couchbase 中并作为查询的返回值,我将发回文档(我的结构):
我的查询:
Update BucketName as e
set start_time="2021-04-27 22:24:31 +0000 UTC",
end_time="2021-11-01 22:24:31 +0000 UTC" where ( my document equality condition)
returning e
并且执行没有错误。
当我试图读取返回的结构时,
我的阅读代码:
var s domain.MyStructSample //
err = result.One(&s)
if err != nil {
if err == gocb.ErrNoResult {
return nil, errors.New("there is no result")
}
logger.ZSLogger.Errorf("error on update one item from my struct with error :%s", err)
return nil, err
}
gocb 在这些时间项上生成错误,错误如下:
"message":"error on update one item from my struct with error :parsing time \"\"2021-11-01 22:24:31 +0000 UTC\"\" as \"\"2006-01-02T15:04:05Z07:00\"\": cannot parse \" 22:24:31 +0000 UTC\"\" as \"T\""}
顺便说一句,正如我所说,更新没有错误地完成(查询执行没有错误)。
那我该怎么办呢?
您是如何生成此查询的:
Update BucketName as e
set start_time="2021-04-27 22:24:31 +0000 UTC",
end_time="2021-11-01 22:24:31 +0000 UTC" where ( my document equality condition)
returning e
如错误所述,存储在 couchbase 中的时间数据应采用 RFC3339 (2006-01-02T15:04:05Z07:00
) 格式而不是默认格式 2006-01-02 15:04:05 -0700 MST
,因此也许您应该使用查询插入数据:
Update BucketName as e
set start_time="2021-04-27T22:24:31Z07:00",
end_time="2021-11-01T22:24:31Z07:00" where ( my document equality condition)
returning e
如果格式化时间有问题,请阅读文档 https://golang.cafe/blog/golang-time-format-example.html
而且,正如@MrFuppes 评论的那样,如果您需要自定义 JSON 输出格式,请阅读此 How to format timestamp in outgoing JSON
我创建了一个结构,它包含两个 time.Time 格式的字段,用 json 标签命名:start_time和end_time.
type MyStruct struct {
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
}
当我尝试使用 gin 框架通过 HTTP 发送 PUT 请求以更新这些值时,我发送的时间格式改变了发送结构。 我要发送的内容:
curl -X PUT -H 'Content-Type: application/json'
http://my_address -d '{
"start_time": "2021-04-27T22:24:31Z",
"end_time": "2021-11-01T22:24:31Z"
}'
它收到什么:
start_time="2021-04-27 22:24:31 +0000 UTC",
end_time="2021-11-01 22:24:31 +0000 UTC",
另一方面, 我将结构保存在 couchbase 中并作为查询的返回值,我将发回文档(我的结构):
我的查询:
Update BucketName as e
set start_time="2021-04-27 22:24:31 +0000 UTC",
end_time="2021-11-01 22:24:31 +0000 UTC" where ( my document equality condition)
returning e
并且执行没有错误。 当我试图读取返回的结构时,
我的阅读代码:
var s domain.MyStructSample //
err = result.One(&s)
if err != nil {
if err == gocb.ErrNoResult {
return nil, errors.New("there is no result")
}
logger.ZSLogger.Errorf("error on update one item from my struct with error :%s", err)
return nil, err
}
gocb 在这些时间项上生成错误,错误如下:
"message":"error on update one item from my struct with error :parsing time \"\"2021-11-01 22:24:31 +0000 UTC\"\" as \"\"2006-01-02T15:04:05Z07:00\"\": cannot parse \" 22:24:31 +0000 UTC\"\" as \"T\""}
顺便说一句,正如我所说,更新没有错误地完成(查询执行没有错误)。 那我该怎么办呢?
您是如何生成此查询的:
Update BucketName as e
set start_time="2021-04-27 22:24:31 +0000 UTC",
end_time="2021-11-01 22:24:31 +0000 UTC" where ( my document equality condition)
returning e
如错误所述,存储在 couchbase 中的时间数据应采用 RFC3339 (2006-01-02T15:04:05Z07:00
) 格式而不是默认格式 2006-01-02 15:04:05 -0700 MST
,因此也许您应该使用查询插入数据:
Update BucketName as e
set start_time="2021-04-27T22:24:31Z07:00",
end_time="2021-11-01T22:24:31Z07:00" where ( my document equality condition)
returning e
如果格式化时间有问题,请阅读文档 https://golang.cafe/blog/golang-time-format-example.html
而且,正如@MrFuppes 评论的那样,如果您需要自定义 JSON 输出格式,请阅读此 How to format timestamp in outgoing JSON