【问题标题】:Unmarshalling time.Time changes the response解组 time.Time 改变响应
【发布时间】:2021-08-24 03:11:03
【问题描述】:

我正在从服务器获取一些数据并取消编组 JSON。当我将日期时间字符串转换为字符串时,我将所有结果都作为 Un-Marshalled 对象,但是当我将其键入为 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",

【问题讨论】:

    标签: go unmarshalling


    【解决方案1】:

    时间“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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-03
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 2022-06-28
      • 1970-01-01
      • 2020-07-19
      相关资源
      最近更新 更多