解组 time.Time 更改响应

Unmarshalling time.Time changes the response

我正在从服务器获取一些数据并解组 JSON。当我将日期时间字符串转换为字符串时,我得到的所有结果都是一个未编组的对象,但是当我将它键入 time.Time 时,对象的其余部分被缩短了。另外,日期不对,可能是提示。

我开始将时间转换为字符串:

type History struct {
    Id      string
    Created string
    Items   []HistoryItem
}

    var response []History
    json.Unmarshal([]byte(s), &response)

太棒了,我得到了这样的列表:

[{91096 2021-06-04T10:28:21.179-0400 [{Rank  Ranked higher}]} {91078 2021-06-04T09:49:28.630-0400 [{Target end  8/Jun/21}]} //...etc

但是当我尝试将其转换为 Time:

type History struct {
    Id      string
    Created time.Time
    Items   []HistoryItem
}

我收到了一件商品,但显然时间不对。该对象中也没有其他值。

[{91096 0001-01-01 00:00:00 +0000 UTC []}]

实际 JSON 表示如下:

"created": "2021-06-04T10:28:21.179-0400",

时间“2021-06-04T10:28:21.179-0400”的格式无法识别。你必须自己解析它。您可以使用这种格式来解析它:

t, err:=time.Parse("2006-01-02T15:04:05.999999999-0700","2021-06-04T10:28:21.179-0400")